Trim paths after xth \ occurence

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Trim paths after xth \ occurence

#1 Post by drgt » 11 Nov 2021 04:39

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Trim paths after xth \ occurence

#2 Post by aGerman » 11 Nov 2021 05:43

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

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Trim paths after xth \ occurence

#3 Post by drgt » 11 Nov 2021 07:23

Yes, but what is being asked is to process each entry in, say, t1.log, and create t2.log with the modified entries.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Trim paths after xth \ occurence

#4 Post by Squashman » 11 Nov 2021 09:03

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?

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Trim paths after xth \ occurence

#5 Post by drgt » 11 Nov 2021 10:16

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 :(

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Trim paths after xth \ occurence

#6 Post by Squashman » 11 Nov 2021 10:24

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?

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Trim paths after xth \ occurence

#7 Post by drgt » 11 Nov 2021 10:35

A "for" within a "for" is partly the answer?

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Trim paths after xth \ occurence

#8 Post by Squashman » 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.

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

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Trim paths after xth \ occurence

#9 Post by drgt » 11 Nov 2021 11:07

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.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Trim paths after xth \ occurence

#10 Post by aGerman » 11 Nov 2021 11:22

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

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Trim paths after xth \ occurence

#11 Post by drgt » 12 Nov 2021 02:19

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

drgt
Posts: 158
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Trim paths after xth \ occurence

#12 Post by drgt » 12 Nov 2021 03:22

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.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Trim paths after xth \ occurence

#13 Post by Squashman » 12 Nov 2021 08:00

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.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Trim paths after xth \ occurence

#14 Post by aGerman » 12 Nov 2021 11:52

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

Post Reply