0

Apreciate any help and excuse me if my terminology is incorrect.

What I am trying to do is write a scrpit/.bat file that will do the following:
copy 1 directory(and subdirectories) from pointA, to point B.
Then in pointB(and subdirectories) unzip the files which will give *.csv files
Then in pointB(and subdirectories) I want to delete some rows from all these csv files

This unix command, run on cygwin, will copy all the files from /cygdrive/v/pointA/* to the current directory . (i.e. the dot is the current working directory)

cp /cygdrive/v/pointA/* .

This unix command, run on cygwin, will go through all the files in the directory and subdirectories that end with .zip and unzip them

find -iname *.zip -execdir unzip {} \;

This unix command, run on cygwin, will go through all the files in the directory and subdirectories that end with .csv
For each file it deletes the 1st 6 rows and the last row and that's the returned file.

find ./ -iname '*.csv' -exec sed -i '1,6d;$ d' '{}' ';'

I was looking to do this in one script/bat file but I am having trouble with the first find command I am having trouble with the find and unzip commands on the one line and am wondering how and if this can be done

chdir C:\pointA
C:\cygwin\bin\cp.exe /cygdrive/v/pointB/* .
::find -iname *.zip -execdir unzip {} \;
::find ./ -iname '*.csv' -exec sed -i '1,6d;$ d' '{}' ';' 

I did try something like this:

C:\cygwin\bin\find.exe -iname *.zip -execdir C:\cygwin\bin\unzip.exe {} \;  

but I get the following:

/usr/bin/find: missing argument to `-execdir'

Can anyone advise if/how this can be done?

1 Answer 1

1

The Cygwin tools use their own kind of paths, e.g. /cygdrive/c/cygwin/bin/unzip.exe though sometimes the Windows paths with backslashes work, the backslashes do tend to confuse the Cygwin tools.

I highly recommend you write your tool in Bash shell script instead of a cmd.exe Windows batch file. In my experience (1) it's much easier to do flow control in bash scripts than in batch files, and (2) the Cygwin environment works better from Bash. You can open a bash shell and run bash yourscript.sh.

Your Bash script might look something like this: (untested)

#!/bin/bash
# This script would be run from a Cygwin Bash shell.
# You can use the Mintty program or run C:\cygwin\bin\bash --login
# to start a bash shell from Windows Command Prompt.

# Configure bash so the script will exit if a command fails.
set -e 

cd /cygdrive/c/pointA
cp /cygdrive/v/pointB/* .

# I did try something like this:

# 1. Make sure you quote wildcards so the shell doesn't expand them
#    before passing them to the 'find' program.
#
# 2. If you start bash with the --login option, the PATH will be
#    configured so that C:\cygwin\bin is in your PATH, and you can
#    just call 'find', 'cp' etc. without specifying full path to it.

# This will unzip all .zip files in all subdirectories under this one.
find -iname '*.zip' -execdir unzip {} \;
Sign up to request clarification or add additional context in comments.

5 Comments

tks. I would be new to bash shell so I will explore that option(but specific pointers welcome) but I was actually interested/hoping in doing it using the commands I already have but my inexperience is probably showing.
Yup, you can start simple with a bash script of one line and add to it until it's a powerful program itself. Usually once a shell script gets to be 4 screens or more on length, I wish I wrote it in Python instead, but never once have I wished I wrote it in Windows cmd.exe batch script.
@ColinDBennett tks but I can't even get the cd command to work. I get the following: '$ bash CopyFilesBash.sh CopyFilesBash.sh: line 6: $'\r': command not found CopyFilesBash.sh: line 9: $'\r': command not found'
@HattrickNZ Bash doesn't usually like scripts that have CRLF (DOS style) line endings. Try using dos2unix CopyFilesBash.sh or dos2unix --d2u CopyFilesBash.sh to convert line endings. Notepad doesn't support LF line endings, so make sure you use a better text editor to edit your scripts.
tks that is useful to know although not sure if that was the issue. but got the basic cd to work, will just work on the rest now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.