Renaming folders that contain exclamation marks

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
josh24
Posts: 3
Joined: 28 Dec 2014 10:47

Renaming folders that contain exclamation marks

#1 Post by josh24 » 28 Dec 2014 11:41

Very new to batch files in general. I'm trying to rename a list folders to the first line of a .txt file within the corresponding folder.

Code: Select all

setlocal enabledelayedexpansion
for /D %%f in (*) do (
set /p str=< %%f\filename.txt
set str=!str:^|=!
echo.!str!
rename "%%f" "!str!")
pause

This works in most cases, but there's a problem when the original folder name has an exclamation mark in it. When this happens, it returns:

Code: Select all

The system cannot find the path specified
|= (or whatever str was last successfully set to)
The syntax of the command is incorrect

When I echo %%f, it outputs the directory with the exclamation marks removed. I think it's a problem related to enabledelayedexpansion, but I don't know how to perform this operation without it. Is there a way to remove exclamation marks from a list of folders?

There's also a problem when the first line of the .txt file contains an asterisk, I tried using

Code: Select all

set str=!str:*=!

but it doesn't work like it did with the vertical bar

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Renaming folders that contain exclamation marks

#2 Post by foxidrive » 28 Dec 2014 12:25

With details of contents of the file you'll probably get a solution.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Renaming folders that contain exclamation marks

#3 Post by Yury » 28 Dec 2014 12:52

josh24 wrote:I think it's a problem related to enabledelayedexpansion,..



josh24, it's true.



josh24 wrote:...but I don't know how to perform this operation without it.



josh24, try it (without "setlocal enabledelayedexpansion"):

Code: Select all

for /d %%f in (*) do (
 set /p str=<"%%f\filename.txt"
 cmd /v:on /c "echo !str:^|=!& rename "%%f" "!str:^|=!""
 )
pause


.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Renaming folders that contain exclamation marks

#4 Post by foxidrive » 28 Dec 2014 13:09

We don't know how the asterisk is getting into the mix, Yury.

josh24
Posts: 3
Joined: 28 Dec 2014 10:47

Re: Renaming folders that contain exclamation marks

#5 Post by josh24 » 28 Dec 2014 14:37

Thanks Yury, it works perfectly.

The first line of filename.txt might have a vertical bar or an asterisk in it. Since these characters aren't allowed in folder names, I'm trying to remove them from the string before I rename the folder. Yury's solution removes the vertical bar, but asterisks still pose a problem

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Renaming folders that contain exclamation marks

#6 Post by Yury » 28 Dec 2014 16:01

josh24, does the first line contain only one asterisk? Try it in this case:

Code: Select all

for /d %%f in (*) do (
 set /p str=<"%%f\filename.txt"
 for /f "tokens=1,2 delims=*" %%g in ('cmd /v:on /c "echo !str:^|=!"') do (
  echo %%g%%h
  ren "%%f" "%%g%%h"
  )
 )
pause


.

josh24
Posts: 3
Joined: 28 Dec 2014 10:47

Re: Renaming folders that contain exclamation marks

#7 Post by josh24 » 28 Dec 2014 17:30

Should be no more than 1 asterisk, so that'll do, Thanks.

Post Reply