Page 1 of 1

Trim paths after xth \ occurence

Posted: 11 Nov 2021 04:39
by drgt
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?

Re: Trim paths after xth \ occurence

Posted: 11 Nov 2021 05:43
by aGerman
Simple tokenizing in a FOR /F ¯\_(ツ)_/¯

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"
Steffen

Trim paths after xth \ occurence

Posted: 11 Nov 2021 07:23
by drgt
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

Posted: 11 Nov 2021 09:03
by Squashman
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?

Re: Trim paths after xth \ occurence

Posted: 11 Nov 2021 10:16
by drgt
It flaters me that you think highly of me.
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
did not work :(

Re: Trim paths after xth \ occurence

Posted: 11 Nov 2021 10:24
by Squashman
Here is the code from one of your previous questions which correctly reads a file.
drgt wrote:
06 Nov 2021 22:14

Code: Select all

for /f "usebackq delims=" %%i in (test.log) do move %%i "C:\path"
What do you think you are missing?

Re: Trim paths after xth \ occurence

Posted: 11 Nov 2021 10:35
by drgt
A "for" within a "for" is partly the answer?

Re: Trim paths after xth \ occurence

Posted: 11 Nov 2021 10:45
by Squashman
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

Posted: 11 Nov 2021 11:07
by drgt
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.

Re: Trim paths after xth \ occurence

Posted: 11 Nov 2021 11:22
by aGerman
drgt wrote:
11 Nov 2021 10:35
A "for" within a "for" is partly the answer?
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)
Steffen

Re: Trim paths after xth \ occurence

Posted: 12 Nov 2021 02:19
by drgt
Squashman wrote:
11 Nov 2021 10:45
Run 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.

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
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.....????

Re: Trim paths after xth \ occurence

Posted: 12 Nov 2021 03:22
by drgt
aGerman wrote:
11 Nov 2021 11:22
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.
Steffen
GOT IT, I think! :oops:
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

Posted: 12 Nov 2021 08:00
by Squashman
drgt wrote:
12 Nov 2021 03:22
Remember that the source path is in double quotes and the target path should be in double quotes.
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

Posted: 12 Nov 2021 11:52
by aGerman
drgt wrote:
12 Nov 2021 03:22
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.
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")
If the content of %%h is quoted in the inner loop, it'll be treated as string like I explained.

Steffen