2

I have been banging my head with this small script to search a delete a file from the remote host. However, I have been able to write to seperate scripts, both to delete and to find a file.

However, now I am trying to merge both and I am unable to use the for loop in the same line as the line which searches the file.

Psexec @IPlist.txt -u ad -p P@$$vv0rCL cmd /c (^WHERE /r D:\ %file%>res.txt

the above line helps to search the file, and below does the deletion part.

del /f /A:H /S /Q "D:\1\e.txt"

Now, since I want to delete all the files, found and stored in rex.txt, I need to run a for loop on this file.

Here is what I am trying to do

Psexec @IPlist.txt -u ad -p P@$$vv0rCL cmd /c (^WHERE /r D:\ %file%>res.txt ^& FOR /f "delims=" %%X IN (res.txt) DO ( del /f /A:H /S /Q "D:\1\e.txt")

but this gives error cmd exit with error code 1, and if I take for loop on next line, then the code doesn't work on remote pc.

Can some one guide please?

Bilal Ahmad
  • 191
  • 1
  • 2
  • 17
  • 1
    Have you considered creating a batch file with your commands in it locally, then copying to the remote server, and using `psexec` to run that batch file, and then delete the remote and local batch files? It might be slightly less efficient, but simplies all the escaping. I also have a vague recollection I've had trouble using the `&` to string commands together using `psexec`. If I started having the slightest bit of complication in the procedure, I found it was easier to build, and for others to maintain, a process which wrote the complexity into a local batch file and pushed it across. – Steven K. Mariner Aug 05 '17 at 16:16

1 Answers1

1

escape the ) in (res.txt) as ^)

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • It didn't change anything, I think issue is with having the for loop in the same line, bcz when I change for loop to next line, all works fine, however del command in for loop doesn't work – Bilal Ahmad Aug 05 '17 at 15:42
  • 1
    I'd add an extra escaped-`)` between the `"` and `)` to balance the opened-( in the `do`. – Magoo Aug 05 '17 at 15:47
  • Psexec @IPlist.txt -u ad -p P@$$vv0rCL cmd /c (^WHERE /r D:\ %n%>res.txt ^& FOR /f "delims=" %%X IN (res.txt) DO ( del /f /A:H /S /Q "D:\1\e.txt"^)) This command gave me an error "system cannot find the file res.txt " exits with error code 2 Just to add if that helps, using this thing, search isn't working either. so both parts of query are not working. – Bilal Ahmad Aug 05 '17 at 15:56
  • Is there any option that I can take for loop into next line and execute delete command there? I try this with echo and it works, however the I am unable to understand how to run delete command , do i need write psexecsvc again for delete command? – Bilal Ahmad Aug 05 '17 at 16:01
  • you were right regarding escaping the parenthesis.... I have successfully escaped all parenthesis, however, now system is unable to find file res.txt. I believe command is trying to find the file on remote host, however the file is placed on my local machine. Any idea on how to counter this please? – Bilal Ahmad Aug 06 '17 at 08:14
  • 1
    I cannot duplicate your setup on my system. I'd try escaping the `>` and possibly specifying a known directory on `D:` – Magoo Aug 06 '17 at 17:07
  • find below script. echo off set /p n="Enter File Name: " echo %n% Psexec @IPlist.txt -u ad -p P@$$vv0rCL cmd /c (^WHERE /r D:\ %n%>res.txt ^& FOR /f "delims=" %%X IN ^(res.txt^) DO ^(del /f /A:H /S /Q %%X ^)) pause Current output: System cannot find file res.txt. Reason might be, that in first part of script, it searches the files and stores the result in res.txt file in the same folder from where script is running, however in for loop it starts looking for res.txt on remote machine where files are to be deleted. If we can find a way around to this it will work. – Bilal Ahmad Aug 07 '17 at 09:01
  • Did you try `...%^>res.txt ... ?` – Magoo Aug 07 '17 at 09:04
  • It produced error for system cannot find the file specified, however I just noticed that it has worked as well, despite producing error. It is deleting the file. – Bilal Ahmad Aug 07 '17 at 09:19
  • First of all, thank you so much for your help and assistance. Error: The system cannot find the path specified. C:\WINDOWS\system32>(del /f /A:H /S /Q D:\2\ah.vbs ) Deleted file - D:\2\ah.vbs The system cannot find the path specified. hH☺°UY1å┤!Σ%!♣0╢t find the path specified. Deleted file - D:\2\New folder\ah.vbs The system cannot find the path specified. C:\WINDOWS\system32>(del /f /A:H /S /Q D:\2\New folder\ah.vbs ) The system cannot find the path specified. Just curious why escaping the file path worked. and need to figure out which file it is trying to find. – Bilal Ahmad Aug 07 '17 at 09:21
  • Escaping a character tells `cmd` that the character is to be treated as an ordinary character, not as its special meaning, hence it forms part of the command to be executed, not the `start` command that is invoking the `psexec`. I'd suggest using a decent editor to view the file produced - it may have unicode characters. You may need to arrange to get the shortnames (assuming the target systems are generating shortnames) – Magoo Aug 07 '17 at 12:35