Trim paths after xth \ occurence
Moderator: DosItHelp
Trim paths after xth \ occurence
A text file contains lines in double quotes.
Within the quotes a path is contained.
Using a batch file I would like to create another text file also containing paths in double quotes, BUT truncating the entries by removing anything beyond the 6th backslash including the 6th backslash.
Can it be done?
Within the quotes a path is contained.
Using a batch file I would like to create another text file also containing paths in double quotes, BUT truncating the entries by removing anything beyond the 6th backslash including the 6th backslash.
Can it be done?
Re: Trim paths after xth \ occurence
Simple tokenizing in a FOR /F ¯\_(ツ)_/¯
Steffen
Code: Select all
for /f "tokens=1-6 delims=\" %%i in ("C:\wherever\you\want\to\go\in\this\particular\case") do echo "%%i\%%j\%%k\%%l\%%m\%%n"
for /f "tokens=6* delims=\" %%i in ("C:\wherever\you\want\to\go\in\this\particular\case") do echo "%%j"
Trim paths after xth \ occurence
Yes, but what is being asked is to process each entry in, say, t1.log, and create t2.log with the modified entries.
Re: Trim paths after xth \ occurence
I read through the majority of your questions and based on that I see no reason why you would not know how to read a file with a FOR /F command or know how to create and/or append to another file.
The code aGerman just gave you shows you a very simple method for breaking up file\folder paths per your requirements.
What is stopping your writing this code yourself?
The code aGerman just gave you shows you a very simple method for breaking up file\folder paths per your requirements.
What is stopping your writing this code yourself?
Re: Trim paths after xth \ occurence
It flaters me that you think highly of me.
In reality I am not worth it.
It is Chinese to me.
did not work
In reality I am not worth it.
It is Chinese to me.
Code: Select all
for /f "tokens=6* delims=\" %%i in ("T1.log") do echo "%%j" >T2.log
Re: Trim paths after xth \ occurence
Here is the code from one of your previous questions which correctly reads a file.
What do you think you are missing?drgt wrote: ↑06 Nov 2021 22:14Code: Select all
for /f "usebackq delims=" %%i in (test.log) do move %%i "C:\path"
Re: Trim paths after xth \ occurence
A "for" within a "for" is partly the answer?
Re: Trim paths after xth \ occurence
Run each of these commands and look at the output. This should help you understand how quotes affect the FOR /F command.
Code: Select all
for /f "delims=" %%G in (T1.log) do echo %%G
for /f "delims=" %%G in ("T1.log") do echo %%G
for /f "usebackq delims=" %%G in ("T1.log") do echo %%G
Re: Trim paths after xth \ occurence
I appreciate it for walking me through it. Maybe this way, I will understand it.
I am not on my pc now but from what I recall it has to do with names with spaces?
After correctly echoing %%G, will proceed with another for to truncate?
I will run the code you posted when I get back but give me a hint for the next step to blend in the code by a German.
I am not on my pc now but from what I recall it has to do with names with spaces?
After correctly echoing %%G, will proceed with another for to truncate?
I will run the code you posted when I get back but give me a hint for the next step to blend in the code by a German.
Re: Trim paths after xth \ occurence
Why? It's so that you want to process a file line-wise rather than to process a single string. A quoted expression in a FOR /F without option USEBACKQ is treated as string. A quoted expression with USEBACKQ is treated as file name. Nothing new here, I told you a few days ago..
Code: Select all
>"t2.log" (for /f "usebackq tokens=1-6 delims=\" %%i in ("t1.log") do echo %%i\%%j\%%k\%%l\%%m\%%n)
Re: Trim paths after xth \ occurence
Squashman wrote: ↑11 Nov 2021 10:45Run each of these commands and look at the output. This should help you understand how quotes affect the FOR /F command.
Thanks a lot! Running your suggestion with the following variation helped me understand.
note: I could not suppress the echoing: "echo test file.log" in, say, block 2. I tried >nul after echo %%x and a "1" appeared.....????Code: Select all
cls @echo 1 for /f "delims=" %%x in (test file.log) do echo %%x @rem The system cannot find the file test. @pause cls @echo 2 for /f "delims=" %%x in ("test file.log") do echo %%x @rem test file.log @pause cls @echo 3 for /f "usebackq delims=" %%x in (test file.log) do echo %%x @rem The system cannot find the file test. @pause cls @echo 4 for /f "usebackq delims=" %%x in ("test file.log") do echo %%x @rem outputs the file lines @pause cls @echo 5 for /f "delims=" %%x in (test.log) do echo %%x @rem outputs the file lines @pause cls @echo 6 for /f "delims=" %%x in ("test.log") do echo %%x @rem test.log @pause cls @echo 7 for /f "usebackq delims=" %%x in (test.log) do echo %%x @rem Outputs the files lines @pause cls @echo 8 for /f "usebackq delims=" %%x in ("test.log") do echo %%x @rem Outputs the files lines
Re: Trim paths after xth \ occurence
GOT IT, I think!
One little problem.
When the source line is exactly 6 levels deep like "C:\1\2\3\4\5" and using %%i\%%j\%%k\%%l\%%m\%%n",
I get "C:\1\2\3\4\5"" (double doublequote at the end).
It is ok when the path is deeper.
Remember that the source path is in double quotes and the target path should be in double quotes.
Re: Trim paths after xth \ occurence
Storing or setting a variable with quotes is not a best practice. Personally I would go back to the code that created your quote surrounded output file and change it so it doesn't output quotes.
Re: Trim paths after xth \ occurence
In this case a nested loop would make sense.
Code: Select all
>"t2.log" (for /f "usebackq delims=" %%h in ("t1.log") do for /f "tokens=1-6 delims=\" %%i in (%%h) do echo "%%i\%%j\%%k\%%l\%%m\%%n")
Steffen