For /F command help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pbesk18
Posts: 3
Joined: 26 Dec 2013 12:15

For /F command help

#1 Post by pbesk18 » 26 Dec 2013 15:33

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) then 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 prevent. 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 52X.

Code: Select all

@echo off

FOR /F "tokens=*" %%A IN (clusters.txt) DO FOR /F "tokens=*" %%B in (assets.txt) DO rcmd \\%%A \vstrmkit\scp del %%B


Here is my list of servers in clusters.txt:

SEA80001-N0
SEA80003-N0
SEA80005-N0
SEA80007-N0
SEA80086-N0
SEA80089-N0
SEA80099-N0
SEA80101-N0
SEA80105-N0
SEA80106-N0
SEA80113-N0
SEA80120-N0
SEA80130-N0
SEA80132-N0
SEA80174-N0


and the list of assets in assets.txt

11E8A51A*
11E8A51D*
11E8A614*
11E88E4E*
11E88E4C*
11E88CE8*
11E88CDA*
11E88B67*
11E87027*
11E86FCF*
11E86F26*
11E86EDB*
11E86E78*
11E8A516*
11E8A7B1*
11E8A7B0*

There are more assets and servers but I just put a list so you guys have an idea of what values I have on my txt files.

Any help would be greatly appreciate it.

Thanks in advance

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For /F command help

#2 Post by foxidrive » 26 Dec 2013 18:06

This creates a temporary batch file to launch that may help delete all the assets

Code: Select all

@echo off
echo.@echo off>delfile.bat
FOR /F "delims=" %%B in (assets.txt) DO (
     >>delfile.bat echo \vstrmkit\scp del "%%B"
)

FOR /F "delims=" %%A IN (clusters.txt) DO (
rcmd \\%%A cmd /c delfile.bat
)
del delfile.bat 2>nul

pbesk18
Posts: 3
Joined: 26 Dec 2013 12:15

Re: For /F command help

#3 Post by pbesk18 » 26 Dec 2013 22:50

Thank you foxidrive ... Can you please explain me how this part will work:

Code: Select all

FOR /F "delims=" %%A IN (clusters.txt) DO (
rcmd \\%%A cmd /c delfile.bat
)
del delfile.bat 2>nul


Sorry but I didn't get how the

Code: Select all

rcmd \\%%A cmd /c delfile.bat
will execute remotely if the batch file was created
locally before connecting to the remote server. Also I need to execute the batch file from \vstrmkit please. So I guess I can just add the following code

Code: Select all

rcmd \\%%A \vstrmkit\cmd /c delfile.bat
is this right?

Thanks Foxie for taking the time to answer this, you also gave me this answer on Stackoverflow, to me it looks like your answer makes the most sense.

Thanks a lot!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For /F command help

#4 Post by foxidrive » 26 Dec 2013 23:03

pbesk18 wrote:Can you please explain me how this part will work:

Code: Select all

FOR /F "delims=" %%A IN (clusters.txt) DO (
rcmd \\%%A cmd /c delfile.bat
)
del delfile.bat 2>nul


Sorry but I didn't get how the

Code: Select all

rcmd \\%%A cmd /c delfile.bat
will execute remotely if the batch file was created
locally before connecting to the remote server. Also I need to execute the batch file from \vstrmkit please. So I guess I can just add the following code

Code: Select all

rcmd \\%%A \vstrmkit\cmd /c delfile.bat
is this right?


I don't know rcmd to be honest.

Does it have the ability to copy a file to the remote system? PsExec can do that, IIRC.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: For /F command help

#5 Post by penpen » 27 Dec 2013 05:08

I don't know rcmd, too.
But if foxidrives solution may not work, i would try something like this:

Code: Select all

rcmd \\Server "(net use N: \\somePC\netShare\subDir)&(for /F %%a in (N:\assets.txt) do (@echo #%%a#))&net use N: /delete"
Assumed that 192.168.0.123 is a server, and somePC is your pc with the assets.txt in a network share within the "subDir" Folder;
you may assign a password to this share, but for a first test it may be suffice to do it this way;
additionally if that works you may replace echo by the needed del command.
Note that N: should not be in use on the server, else you have to use another volume name.

penpen

pbesk18
Posts: 3
Joined: 26 Dec 2013 12:15

Re: For /F command help

#6 Post by pbesk18 » 27 Dec 2013 09:09

foxidrive wrote:Can you please explain me how this part will work:

I don't know rcmd to be honest.

Does it have the ability to copy a file to the remote system? PsExec can do that, IIRC.
[/quote]

Foxidrive here it's what I get went I run the command, I can try psExec.... RCMD allows me to connect to a remote server but I can't connect
to another server from the remote one. It has that limitation.

Unfortunately your batch didn't work since it couldn't find the bat file on the remote server:

Code: Select all

C:\Temp\Deletes>test.bat
Executing on \\sea80003-n0: cmd /c delfile.bat

'delfile.bat' is not recognized as an internal or external command
operable program or batch file.

Remote server \\sea80003-n0 disconnected

C:\Temp\Deletes>

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For /F command help

#7 Post by foxidrive » 27 Dec 2013 18:54

Try this: though I'm not sure how \vstrmkit\scp is being used and the current directory - but del can take multiple filespecs.
The limitation will be around 8K line lengths for XP and higher.

Code: Select all

@echo off
set file=
FOR /F "delims=" %%a in (assets.txt) DO call set file=%%file%% "%%a"
FOR /F "delims=" %%a IN (clusters.txt) DO rcmd \\%%a \vstrmkit\scp del %file%

Post Reply