1

I have a number of files that contain a line that needs to be deleted. These files are contained in subdirectories of a specific directory, and all files of a specific extension need to be iterated through and the line deleted. The problem I have is that the path has brackets and spaces, which seem to prevent the loop from working properly. How do you escape the path?

The code I have currently:

For /R "%ALL_BUT_FIRST%\bin\%1\Scripts" %%G in (*.csx) do (
echo %%G 
findstr /V "texttoreplace" "%%G" >"%%G"
)

When not trying to write to the file, findstr outputs to the console the correct result perfectly. It is only writing the file that causes the .bat to fail with the error:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(5185,5): error MSB3073: The command ""D:\Users[username]\Desktop\Other projects (ie open source ones)\Freee\FrEee.WinForms\post-build.bat" Debug D:\Users[username]\Desktop\Other projects (ie open source ones)\Freee\FrEee.WinForms\" exited with code 1.

  • Can you place an * (esterisk) into it to fill the rest of the line ? – vssher Jan 8 at 17:18
  • @vssher add in where sorry? – Imperial Justinian Jan 8 at 17:42
  • FrEee.Core*.dll Wherever there are spaces, etc. – vssher Jan 8 at 17:45
  • The spaces don't seem to be the problem. When running the command without the >"%%G" it prints to console the file as expected. Otherwise, it errors. – Imperial Justinian Jan 8 at 17:47
  • Have you tried a page like this one, maybe it can be helpful: ss64.com/nt/for_r.html – vssher Jan 8 at 18:04
1

Use %%~fG in your for loop to expand to a fully qualified path.

CD "%ALL_BUT_FIRST%\bin\%1\Scripts"
For /R %%G in ("*.csx") do (
echo %%~fG 
findstr /V "texttoreplace" "%%~fG" >"%%~fG"
)
| improve this answer | |
  • C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(5185,5): error MSB3073: The command ""D:\Users[username]\Desktop\Other projects (ie open source ones)\Freee\FrEee.WinForms\post-build.bat" Debug D:\Users[username]\Desktop\Other projects (ie open source ones)\Freee\FrEee.WinForms\" exited with code 1. The path is already fully qualified. – Imperial Justinian Jan 9 at 0:34
  • The directory path yes, The File path, No. The answer provided expands each FILE path in your loop to a fully qualified File path, resolving spaces, parenthesis etc. – T3RR0R Jan 9 at 1:04
  • If your still experiencing issues with outputing to the file using findstring, This is likely the solution: stackoverflow.com/questions/52828691/… – T3RR0R Jan 9 at 1:08
  • echo %%~fG and echo %%G produce identical outcomes in the console, which would seem to me to mean it is already fully qualified file path. Unfortunately, the issue is not one with findstr but with the path name. When replacing last part with >temp.txt it works perfectly. – Imperial Justinian Jan 9 at 1:24
  • When find string is substituted with echo text >>"%%~fG" it also works fine, but Suggesting it is Not the file path. – T3RR0R Jan 9 at 1:51
1

So after several days of working on this, and a comment from T3RR0R in his answer, I found the solution to my problem. It turns out the issue was not the file path, but that the file could not be overwritten.

The solution is:

CD "%ALL_BUT_FIRST%\bin\%1\Scripts"
For /R %%G in ("*.csx") do (
echo %%~fG.temp
echo %%G
ren "%%~fG" "%%~nxG.temp"
findstr /V "texttoreplace" "%%G.temp" > "%%~fG"
del "%%G.temp"
)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.