How to create a loop/skip in batch - loops

I am trying to download a chunk of files from an application. The shell command for it is 'go filename download'.
I have a text file containing all the filenames I have to download. All I want to do is to run a script/command such that when the above command is executed
1. the filenames are picked up from the textfile & executed using the above command
2. existing files/unavailable files are skipped
3. the process then continues with the next files in the list
So far I have this idea of using an operator like go $ download & then feed the operator with the text file containing the filenames list. Thanks in advance.

For Windows, you can use for /f to process the file and create a command from it. The following script supergo.cmd shows how this can be done:
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f "delims=" %%f in (list.txt) do (
echo go "%%f" download
)
endlocal
The following transcripts shows it in operation:
C:\Pax> type list.txt
file1.txt
file number 2.txt
another file.jpg
C:\Pax> supergo
go "file1.txt" download
go "file number 2.txt" download
go "another file.jpg" download
If you're using a shell like bash, you can use sed to create a temporary script from the input file then run that:
#!/bin/bash
sed -e "s/^/echo go '/" -e "s/$/' download/" list.txt >/tmp/tempexec.$$
chmod u+x /tmp/tempexec.$$
. /tmp/tempexec.$$
rm -rf /tmp/tempexec.$$
This puts an echo go ' at the start of each line, a ' download at the end, then marks it executable and executes it.
In both cases (Windows and bash), remove the echo to get it to do the real work.

Related

set /p failing to read sftp mapped file

I'm trying to write a batch file to act as an interface between Source-Insight, and Git running on a Linux server, but I'm running into an issue where the set /p does not seem to be working as advertised.
The batch file is supposed to run a linux script (via plink), which will check out the appropriate files into two directories, and then invoke Beyond Compare to compare the directories (note, these are on an sftp mounted drive so that dos can see them). The directory names are dynamic, so I need the batch file to read the generated directory name from a file before passing it to Beyond Compare. I can't seem to get this working...
I have the following lines in my batch script:
#echo on
plink server1234 -l %user% -i %ppk_file% "cd %root%; ~/bin/compare_git_all.sh --debug --ref"
echo "set /p dir=<%dosroot%\.comparegit.dosdir"
set /p dir=<%dosroot%\.comparegit.dosdir
echo dir="%dir%" (from %dosroot%\.comparegit.dosdir)
"C:\Program Files\Beyond Compare 4\BCompare.exe" %dir%.refpt %dir%
#echo off
My output ends up being:
"set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir"
dir="" (from z:\builddir\pd2\wt1\.comparegit.dosdir)
So first issue (annoyance really), is that #echo on is not causing the commands to be echoed (which according to the pages I've google it's supposed to do...)
But what's killing me is that %dir% seems to be blank. I've verified that the file contains the data I am looking for:
C:\>more z:\builddir\pd2\wt1\.comparegit.dosdir
z:\builddir\pd2\wt1\.comparegit.zmP8BK
if I run the same command from a Command Prompt, I get:
C:\>set dir=blank
C:\>echo %dir%
blank
C:\>set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir
C:\>echo %dir%
z:\builddir\pd2\wt1\.comparegit.zmP8BK
So, I'm missing something, but I'm not sure what it is. Any help would be appreciated. (note, if it makes any difference, the batch file is being invoked from a keymapping within Source Insight)

Run command with drag and drop onto batch file

I want to drag and drop a file onto a batch file in order to run the below command on it. How do I go about running the command on the dropped file?
PotreeConverter.exe <dropped file> -o C:/output -p index
The path of the file, when you drop it on the BATfile, will be returned as a normal %1 argument.
so :
#echo off
PotreeConverter.exe "%~1" -o C:/output -p index
You can use %* if you drop more then 1 file
Example :
#echo off
for %%a in (%*) do echo [%%a] was dropped on me
pause
Following this easy guide.
Create a batch file test.bat with the contents
#echo off
echo The full path of the file is: %1
pause
Drag any file onto it, you will see that %1 is replaced with the full path for that file in quotes.
Now you know how to execute some command that takes a path to a file as an argument:
#echo off
some_command_that_takes_a_path_to_a_file %1

Need to parse the current directory a batch file is in into a variable I can use in said batch file

Working with rsync on a windows machine (cWrsync) and I am syncing a large folder with sub directories to a web server which takes several minutes to complete. But when the user just needs to add a file to a sub-directory I don't want them to have to wait an hour to sync that directory.
My Idea
Add a file into each sub-directory (1_sync.bat) that they can execute when they add or delete a file within that directory. I need the batch file to be able to dynamically tell rsync which directory to sync.. here is the static version:
#echo off
REM Make environment variable changes local to this batch file
SETLOCAL
REM where ti find rsync and related files
SET CWRSYNCHOME=C:\cwRsync
SET HOME=C:\Users\greg\AppData\Roaming
SET CWORLDPATH=%PATH%
SET PATH=%CWRSYNCHOME%\BIN;%PATH%
"C:\cwRsync\bin\rsync.exe" -v -e 'ssh -i C:\home\greg\.ssh\id_rsa' --delete --recursive --inplace "/cygdrive/z/1CustomerDocs/2017/Client Folder/" "root#domainname.com:/var/storage/customer_files/2017/Client\ Folder/"
In the above example I would like to have Client Folder be a variable that will detect what folder the batch script is actually in so I can just through one of the bat files in every sub directory.
I tried %~dp0 which almost does the trick, but outputs the entire path.. I just need the last two directories.
so if %~dp0 = \SERVER-PATH\Content\1CustomerDocs\2017\Client Folder\
I wish I could cut the last two directories off and have a variable that looks like
2017/Client Folder (but also need one that escapes the spaces for linux)
So the end results would look like
#echo off
REM Make environment variable changes local to this batch file
SETLOCAL
REM where ti find rsync and related files
SET CWRSYNCHOME=C:\cwRsync
SET HOME=C:\Users\greg\AppData\Roaming
SET CWORLDPATH=%PATH%
SET PATH=%CWRSYNCHOME%\BIN;%PATH%
SET CUST_FOLDER_WINDOWS=*YOUR HELP NEEDED HERE*
SET CUST_FOLDER_LINUX=*YOUR HELP NEEDED HERE*
"C:\cwRsync\bin\rsync.exe" -v -e 'ssh -i C:\home\greg\.ssh\id_rsa' --delete --recursive --inplace "/cygdrive/z/1CustomerDocs/%CUST_FOLDER_WINDOWS%" "root#domainname.com:/var/storage/customer_files/%CUST_FOLDER_LINUX%"
And again, I would need the linux folder to escape spaces.
Thanks for the help!
To be independent from the current dirlevel:
#echo off
Echo current dir %CD%
Echo Batch dir %~dp0
for %%a in ("%~dp0.") Do Set "Parent=%%~nxa"
for %%a in ("%~dp0..") Do Set "Grandparent=%%~nxa"
Echo Last 2 dirs \%Grandparent%\%Parent%
current dir Q:\Test\2017\08\10
Batch dir Q:\Test\2017\08\10\
Last 2 dirs \08\10
Figured it how below
#echo off
REM Make environment variable changes local to this batch file
SETLOCAL
REM where ti find rsync and related files
SET CWRSYNCHOME=C:\cwRsync
SET HOME=C:\Users\greg\AppData\Roaming
SET CWORLDPATH=%PATH%
SET PATH=%CWRSYNCHOME%\BIN;%PATH%
for /F "tokens=5,6 delims=\" %%a in ("%0") do (
"C:\cwRsync\bin\rsync.exe" -v -e 'ssh -i C:\home\greg\.ssh\id_rsa' --delete --
recursive --inplace "/cygdrive/z/1CustomerDocs/%%a/%%b/"
"root#domainname.com:/var/storage/customer_files/%%a/%%b/"
)

Command Batch Script In Context Menu not working

I have a old batch script from nt/xp that runs from Context Menu. What it does is when I select a folder and run cmd it will create a temp folder in the active folder i right clicked in. Then will run a program to convert all the tiff's in the original folder and output the new images in the temp folder. New that I using windows 7, I'm having problems getting the CMD.exe to open in the working folder. when I use the script and right click it goes to /windows/system32 and not the folder I click on.
here are the reg file and batch to show what I want to do:
REG file:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Folder\shell\BW Comp/OV]
#="B&W Compress/OV"
[HKEY_CLASSES_ROOT\Folder\shell\BW Comp/OV\Command]
#="C:\\Program Files\\ISRU\\bin\\bwcov.cmd"
BATCH file:
mkdir temp
FOR %%j in (*.tif) do mr_file -T -S 128 -C j -Q 3 -K g %%~nj.tif temp\%%~nj.tif
This was a very simple setup but now with window 7 I can't get it to use the working folder in the batch when making DIR or processing images.
Try this batch file:
#echo off
pushd "%~1"
mkdir temp
FOR %%j in (*.tif) do mr_file -T -S 128 -C j -Q 3 -K g "%%~nj.tif" "temp\%%~nj.tif"
popd
if mr_file is a batch file also then it will need call before the name.
This batch file should work in the SENDTO menu also
but the registry file looks odd to me.
Foxidrive, I tried your suggestion and that work creating temp folder using folder I right clicked on.
Here are the new files.
Batch File (I used a new program called make_pry instead of mr_file:
#echo on
pushd "%~1"
Title %~f1
mkdir temp
FOR %%j in (*.tif) do make_pyr %%~nj.tif -TIFF -JPEG -QFACTOR 97 -tile 128 -out temp\%%~nj.tif
And Reg File (this file was also changed and was the only way I could get it to mkdir command to work in batch. If I remove the %1, /a or /c it will not work:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Folder\shell\BWCOV]
#="BW Compress OV"
[HKEY_CLASSES_ROOT\Folder\shell\BWCOV\command]
#="cmd.exe /a /c Inpho_bwcov.cmd %1"

DOS Batch file - Copy file based on filename into folder

I would like to use a batch file to put them into default folder, but the account name is in the middle of the folder. Have any script I can use in dos command prompt?
888123AA.pdf
888123BB.pdf
888123CC.pdf
777456AA.pdf
777456BB.pdf
777456CC.pdf
Default folder:
999-888123-03
666-777456-01
#echo off
setlocal EnableDelayedExpansion
rem Process all .pdf files
for %%a in (*.pdf) do (
rem Get just the file name, ie: "888123AA"
set fileName=%%~Na
rem Using the file name minus two last chars, ie: "888123"
rem get the default folder with that name
for /D %%b in (*-!fileName:~0,-2!-*) do (
rem And copy the file to that folder
copy "%%a" "%%b"
)
)
I don't remember any apparent way to do it other than a UNIX shell... Maybe get MSYS and use that (outdated) bash to help?
Here is a bash script that can use after you installed bash from MSYS (or you can sort it with a Linux box - Ubuntu is no bigger than 800MB and can run as LiveCD without interfering your current Windows system, and the LiveCD can double as a system saver when needed. :-)
#!/bin/bash
for each in ./*; do
if [ -d $each ]; then # Only folders are minded.
# Extract the second part of the folder name.
ACCOUNT_NAME=`echo $each | sed "s/\\-/\n/" | head -n 2 | tail -n 1`
cp -v ./$ACCOUNT_NAME*.pdf $each
fi
done

Resources