For Command help - nested command - batch-file

I have created the following simple batch file.... What I'm trying to accomplish here is to connect using Remote Command (RCMD utility) to a list o servers defined in (clusters.txt) and delete a list of video files difines in (assets.txt)... I want to delete those assets on every server. This Batch file works however it has to connect and disconnect to delete every piece of asset and that's exactly what I'm trying to avoid. I want to be able to connect to 1 server and delete my list of assets %%B using scp del cmd then disconnect from that server and go to the next one on the list %%A and delete all the assets from there with just one connection instead of having to connect and disconnect 200 times.
#echo off
FOR /F "tokens=*" %%A IN (clusters.txt) DO FOR /F "tokens=*" %%B in (assets.txt) DO rcmd \\%%A \vstrmkit\scp del %%B
This is what the list of files looks like:
11E8A51A*
11E8A51D*
11E8A614*
11E88E4E*
11E88E4C*
and the list of server it's just computer hostnames with letters and numbers:
SEA88630-N0
Any help would be greatly appreciate it.

Without knowing the server and client versions of the OS, it is possible to have some kind of command line limitations. But, let's try
#echo off
rem prepare environment
setlocal enableextensions enabledelayedexpansion
rem Define the command that will be executed for each element
set "command=echo \vstrmkit\scp del"
rem Prepare the command for the list of files into a environment variable
set "command=set c=%command% "
for /f "tokens=*" %%f in (assets.txt) do set "command=!command!/#c#%%~f"
rem Adjust the command variable to generate a valid concatenation of commands
set "command=%command:/=&%"
rem Connect to each of the servers sending the generated command
rem In the last step, change the # with ! to generate on remote, when
rem delayed expansion is applied, the correct final command to execute
setlocal disabledelayedexpansion
for /f "tokens=*" %%a in (clusters.txt) do rcmd \\%%a cmd.exe /v:on /c "%command:#=!%"
Note that the line where the main command in defined, there is a echo inserted for testing. If everything seems ok, remove echo to do it work.
EDITED - Option number 2
It seems that there is some problem with quotes. The previous code works in my system but not in the OP. Let's try another one. It also works on my system.
#echo off
rem prepare environment
setlocal enableextensions disabledelayedexpansion
rem Define the command that will be executed for each element
set "command=echo \vstrmkit\scp del"
rem Define a temporary file to contain commands to process
set "tempFile=%temp%\%~nx0.tmp"
rem Generate the content of the temp file with the list of commands
rem to execute for each server
(
for /f "tokens=*" %%f in (assets.txt) do echo %command% %%f
echo exit
) > "%tempFile%"
rem Connect to each of the servers sending the generated command list
for /f "tokens=*" %%a in (clusters.txt) do (
rcmd \\%%a < "%tempFile%"
)
del /f /q "%tempFile%" > nul 2>nul
endlocal

#ECHO OFF
SETLOCAL
SET "filelist="
FOR /f "delims=" %%A IN (assets.txt) DO CALL SET filelist=%%filelist%% %%A
FOR /F "tokens=*" %%A IN (clusters.txt) DO echo rcmd \\%%A \vstrmkit\scp del %filelist%
GOTO :EOF
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO rcmd to rcmd to execute the deletion
Possible solution if the command can't take multiple arguments:
Create a batch file called delasset.bat and copy it either into \vstrmkit on each target machine or some other directory on the path of each machine (eg c:\windows or c:\windows\system32)
delasset.bat should contain
#ECHO OFF
ECHO DEL %*
(Again, I've simply ECHOed the DEL - you'd need to remove the echo after verifying to activate)
Then change ...rcmd \\%%A \vstrmkit\scp del %filelist% to ...rcmd \\%%A \vstrmkit\scp DELASSET %filelist%
(it's case-insensitive; I've UPPERCASED it for emphasis)
Another possible way:
#ECHO OFF
SETLOCAL
SET "filelist="
FOR /f "delims=" %%A IN (assets.txt) DO CALL SET filelist=%%filelist%%:%%A
SET filelist=%filelist:~1%
FOR /F "tokens=*" %%A IN (clusters.txt) DO echo rcmd \\%%A \vstrmkit\scp del %filelist::=^&\vstrmkit\scp del %
GOTO :EOF
which should generate (eg)
rcmd \\SEA88630-N0 \vstrmkit\scp del 11E8A51A*&\vstrmkit\scp del 11E8A51D*&\vstrmkit\scp del 11E8A614*&\vstrmkit\scp del 11E88E4E*&\vstrmkit\scp del 11E88E4C*
Note that filelist is now built with colons separating the items. Colon cannot be part of a filename. The first character (which will be a colon, since filelist has :filenamefromfile appended to it) is then stripped off (see set/? from the prompt for substring documentation)
The hieroglypics in %filelist::=^&\vstrmkit\scp del % mean 'with filelist, substitute for (first :) [the characters between the first : and the = - which happens to be a colon] with the string &\vstrmkit\scp del (ie. all characters between the = and the closing % - with the additional modification that since & is a special character to cmd (it means 'do the thing before the &, then the thing after') the & needs to be "escaped" by a caret (^) which says "this is a special character that I want to use as an ordinary character".
Leaving the echo firmly in place will allow you to see precisely what should be delivered. Sadly, I've no idea whether it's correct...

Something like this should help.
I don't now for what you have an * after the file-name but it have to be removed by
the script. Or is that a .* ???
EDIT : IF you have various file with the same name and various ("Unknown") extensions then you have to test for the extensions with a new FOR loop :
#echo off
setlocal EnableDelayedExpansion
FOR /F "tokens=*" %%A IN ('type clusters.txt') DO (
FOR /F "tokens=*" %%B in ('type assets.txt') DO (
set $asset=%%B
rcmd.exe \\%%A \vstrmkit\scp del !$asset:~0,-1!.*))

Related

Using %~dp0 on non-alphanumeric characters [duplicate]

I usually get the current working directory by giving the batch command %~dp0 for combine multiple csv files.
But I encountered ampersand (&) symbol while getting current working directories which makes batch file broke after the '&' saying the path after & is not recognized as an internal or external command.
Can any of you guys please help me in modifying my below script in identifying & and replacing it with ^& (as this can escape the & and run the batch file). Any of your suggestions is appreciated;
Here is my code:
#echo off
ECHO Set working directory
pushd %~dp0 - How to escape & in this line???
ECHO Deleting existing combined file
del combined.csv
setlocal ENABLEDELAYEDEXPANSION
REM set count to 1
set cnt=1
REM for each file that matches *.csv
for %%i in (*.csv) do (
REM if count is 1 it's the first time running
if !cnt!==1 (
REM push the entire file complete with header into combined.csv - this will also create combined.csv
for /f "delims=" %%j in ('type "%%i"') do echo %%j >> combined.csv
REM otherwise, make sure we're not working with the combined file and
) else if %%i NEQ combined.csv (
REM push the file without the header into combined.csv
for /f "skip=1 delims=" %%j in ('type "%%i"') do echo %%j >> combined.csv
)
REM increment count by 1
set /a cnt+=1
)
cmd \k
Putting quotation marks around %~dp0 should be enough:
...
pushd "%~dp0"
...
Btw, I don't understand why you use pushd. You don't popd anywhere in your code so pushd seems useless. If I understand ECHO Set working directory correctly, you should replace pushd with CD %~dp0.

Creating List of Folders from File Names

I have a folder containing many files named as such: JBMA_23456.docx, JMRI_21456.docx, CM_22554.docx, QUA_11224.docx. How do I create a sub-folder for each file bearing the same name as the file but without the .docx file extension? Additionally, I want to store the filename only as variables.
For example, I need to create a sub-folder named JBMA_23456 from the document
JBMA_23456.docx. Can anyone point me in teh right direction?
#echo off
for %%A in (*.docx) do if not exist "%%~nA" md "%%~nA"
This creates a folder with the same name as each .docx file.
View modifiers in for /? or in call /?. The n modifier is the name.
Path modifiers:
dpnx is drive, path, name and extension.
Here's an example batch file which attempts to perform the tasks as laid out in your question:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "BaseDir=C:\Users\Compo\Desktop\test"
Set "FileExt=.docx"
For /F "Delims==" %%A In ('Set var[ 2^>Nul') Do Set "%%A="
Set "i=0"
For /F "Delims=" %%A In ('Where "%BaseDir%":*%FileExt% 2^>Nul') Do (Set /A i+=1
Call Set "var[%%i%%]=%%~nA"
If Exist "%%~dpA%%~nA\" (Echo Sub-Folder %%~nA already exists in %BaseDir%
) Else (Set /P "=Creating sub-folder %%~nA in %BaseDir%"<Nul
MD "%%~dpA%%~nA">Nul 2>&1 && (Echo= was successful) || Echo= failed))
Set var[ 2>Nul
Pause
In order to use it, you would first ensure that the directory holding your files, (excluding any trailing backslash), is placed between the = and " on line 4, and the single file extension, (including the leading period, .), similarly on line 5.
It is not entirely clear what you are asking for...
Anyway, here is the code i made
echo off
chcp 65001
cls
for /f "usebackq delims=." %%0 in (`dir /b "*.docx"`) do (
set filename=%%0
md %filename%
)
cmd /k
It creates a new folder for every file. Feel free to ask if this was not what you expected

Search file with wildcard path

I want to write a script to prompt user for file path and list all files found. The file path can contain wildcards. Something similar to this. But the batch script version of it. For example:
C:\Somewhere\user*\app\version-*.*\start.exe
The files might be located like this:
C:\Somewhere\user345\app\version-1.0\start.exe
C:\Somewhere\user898\app\version-1.2\start.exe
C:\Somewhere\user898\app\version-1.3\start.exe
I tried to use FOR and it turns out to be so much harder than expected because FOR does not support wildcards in the middle of a path.
Is there a way to list these files? (Maybe without using for?)
I think this recursive solution works pretty well; you may name it WCDIR.bat:
#echo off
setlocal
if "%~1" neq "" set "next=%~1" & goto next
echo Show files selected by several wild-cards
echo/
echo WCDIR wildcardPath
echo/
echo Each folder in the path may contain wild-cards
echo the last part must be a file wild-card
goto :EOF
:next
for /F "tokens=1* delims=\" %%a in ("%next%") do set "this=%%a" & set "next=%%b"
if defined next (
for /D %%a in ("%this::=:\%") do (
setlocal
cd /D "%%~a" 2>NUL
if not errorlevel 1 call :next
endlocal
)
) else (
for /F "delims=" %%a in ('dir /B /A:-D "%this%" 2^>NUL') do echo %%~Fa
)
exit /B
EDIT: I fixed a small bug in the last for /F command.
For example, the output of WCDIR.bat C:\Windows\Sys*\find*.exe command in my Windows 8.1 64-bits computer is:
C:\Windows\System32\find.exe
C:\Windows\System32\findstr.exe
C:\Windows\SysWOW64\find.exe
C:\Windows\SysWOW64\findstr.exe
You can try with the command Where /?
The WHERE command is roughly equivalent to the UNIX 'which' command. By default, the search is done in the current directory and in the PATH.
#echo off
Where /R "%programfiles%" *winrar.exe
pause
#echo off
:: Example d'input
set UserInput=*drive*
:: building the Pattern
set cmd=%Userinput%.exe
:: storage Where.exe command in a macro, the execution will be faster
set whereCmd=where.exe /r c:\windows\ %cmd%
:: execution of macro and output formatting
for /f %%a in ('%whereCmd%') do echo %%~nxa --^> %%a
pause

Batch - Search for part/exact name and copy line from text file into batch as var

This information below is contained in a text file and formatted as such.
/var/www/xxx/html/videos/video_folder_1
/var/www/xxx/html/videos/video_folder_2
/var/www/xxx/html/videos/video_folder_3
/var/www/xxx/html/videos/video_folder_4
/var/www/xxx/html/videos/video_folder_5
/var/www/xxx/html/videos/video_folder_6
/var/www/xxx/html/videos/video_folder_7
I also have a variable called %file_name% in the batch file already defined.
So lets say that is it is %file_name% = V001-video_folder_6.mp4
As you can see there is some more extra information, V001- and .mp4.
I would like to use the var %file_name% to search the text file and return the entire line. In this case it would return /var/www/xxx/html/videos/video_folder_6 and then put this information in a new var, let us say, %folder_path%.
I think I would use findstr however I have been playing around and not getting the best results.
The problem with the methods that use findstr is that they are slow, because they require to execute findstr.exe (a ~30KB file) each time. A simpler/faster solution is to use just internal Batch commands with the aid of an array. If the number of names to process is large, the difference in time between the two methods may be marked.
#echo off
setlocal EnableDelayedExpansion
rem Load the lines from text file into an array with the last part as index:
for /F "delims=" %%a in (test.txt) do (
set "line=%%a"
for %%b in (!line:/^= !) do set "lastPart=%%b"
set "folder[!lastPart!]=%%a"
)
set "file_name=V001-video_folder_6.mp4"
rem Get the folder from file_name:
for /F "tokens=2 delims=-." %%a in ("%file_name%") do set "folder_path=!folder[%%a]!"
echo Folder path is: %folder_path%
Let us assume the posted lines are in file Test.txt in current working directory.
#echo off
set "file_name=V001-video_folder_6.mp4"
for /F "tokens=2 delims=-." %%A in ("%file_name%") do set "folder=%%A"
for /F "delims=" %%P in ('%SystemRoot%\System32\findstr.exe "/C:%folder%" Test.txt') do (
set "folder_path=%%P"
goto NextCommand
)
:NextCommand
echo Full folder path is: %folder_path%
Open a command prompt window, enter the command for /?, hit key RETURN or ENTER and read output help to understand this little code.
The command goto inside FOR loop results in an immediate exit from loop processing output of findstr.exe after first found line containing the folder path of interest.
Perhaps better in case of searched folder is not found in text file:
#echo off
set "file_name=V01-VIDEOS for school (Miss Patrick).mp4"
for /F "tokens=2 delims=-." %%A in ("%file_name%") do set "folder=%%A"
for /F "delims=" %%P in ('%SystemRoot%\System32\findstr.exe "/C:%folder%" Test.txt') do (
set "folder_path=%%P"
goto FoundFolder
)
echo "%folder%" not found in file Test.txt.
pause
goto :EOF
:FoundFolder
echo Full folder path is: "%folder_path%"
pause
This should work:
::file_name=V001-video_folder_6.mp4
::file containing folder paths is called paths.txt
for /f "tokens=2 delims=-." %%a in ("%file_name%") do set FN=%%a
for /f %%a in ('findstr /E /L "%FN%" "paths.txt"') do set folder_path=%%a
echo %folder_path%
Which does what you want in effectively two lines.

Rename multiple files in a directory using batch script

I have about 1000 images and they have name like "IMG-12223". I want to rename them to 1 2 3 4 ... 1000. How can I do that. I have written a batch script which list the files but I don't know how to rename each file. e.g. rename first image with name "IMG-12223" to 1 , second image with name "IMG-23441" to 2 and so on ...
for /r %%i in (*) do (
echo %c%
)
Here's the script. Just put the script in your folder and run it.
#echo off & setlocal EnableDelayedExpansion
set a=1
for /f "delims=" %%i in ('dir /b *') do (
if not "%%~nxi"=="%~nx0" (
ren "%%i" "!a!"
set /a a+=1
)
)
If you want to keep the extensions, i.e. rename "IMG-12223.jpg", "IMG-12224.jpg", etc to "1.jpg", "2.jpg", etc, you may use the following script.
#echo off & setlocal EnableDelayedExpansion
set a=1
for /f "delims=" %%i in ('dir /b *.jpg') do (
ren "%%i" "!a!.jpg"
set /a a+=1
)
[Update] Here're explanations for the lines mentioned in Jack's comment.
setlocal EnableDelayedExpansion
In general, we want the variable a to be delayed expansion when it's executed but not the line is read. Without it, the variable a cannot get its increased value but always 1.
For the detail of EnableDelayedExpansion, please refer to the answer https://stackoverflow.com/a/18464353/2749114.
for /f "delims=" %%i in ('dir /b *.jpg')
Here dir with /b option, lists only file names of all jpg files.
The for loop traverses and renames all jpg files.
For the delims option, since the default delimiter character is a space, without the option delims=, it fails with the image files with spaces in the file names. I.E. for an image file named "img with spaces.jpg", without the option, the value of %%i is "img" but not the whole name "img with spaces.jpg", which is incorrect.
For for loop, please refer to the page http://ss64.com/nt/for_f.html.
if not "%%~ni"=="%~n0"
I have change it to if not "%%~nxi"=="%~nx0" to be more accurate. And the codes attached have been updated.
It's actually used to avoid to rename the bat file itself. If we limit the renaming only upon "jpg" files, then the line is not needed.
%%~nxi is the file name with extension for each file traversed. And %~nx0 is the running bat file with extension. For details, please refer to the page DOS BAT file equivalent to Unix basename command?.
There is no need for a batch script. A simple one liner from the command line can do the job :-)
I use DIR /B to generate the list of files, piped to FINDSTR to number the files, all enclosed withn FOR /F to parse the result and perform the rename.
for /f "delims=: tokens=1*" %A in ('dir /b *.jpg^|findstr /n "^"') do #ren "%B" "%A%~xB"
Double the percents if you want to put the command in a batch script.
Try this, you have pair of namevalues in a text file then loop values and do the magic. Namevalues are separated by empty spaces. This allows you to map old->new filenames accordingly. Or you keep idx+1 counter and use it for new filenames.
keyvalue.bat
#echo off
set idx=0
for /F "tokens=1,2" %%A in (keyvalue.txt) do call :PROCESS "%%A" "%%B"
GOTO :END
:PROCESS
set var1=%~1
set var2=%~2
set /A idx=%idx%+1
echo %var1% goes to %var2% (%idx%)
GOTO :EOF
:END
pause
keyvalue.txt
file888.dat newfile1.dat
file333.dat newfile2.dat
file9.dat newfile3.dat
file01.dat newfile4.dat

Resources