0

I have some folders that are named a specific way:

Administration

LS01....
LS02....
LS03....

I want to create a batch file that is located in that folder. When the batch file is started, it goes through the names of the folders and changes the LS to SW, uses the numbers and removes the excess name.

I have no experience with batch-coding, but tried my best. This is what I could come up with until now:

for /D %%f in ("%cd%\*") do (
   set "name=%%~nf"
   set name=%name:LS=%
   rename "%%f" "SW_%name%"
   pause.
)

I don't understand why the setting of the variable name does not work. The folders get renamed but only with SW_. The number, which should be in the variable name, does not show up.

Could you please help me out with my code ?

Thank you for your attention.

Best regards

Sam

PS: I'm new to stackoverflow

Gerhard
  • 22,678
  • 7
  • 27
  • 43
Samuel
  • 3
  • 1

1 Answers1

0

You need delayedexpansion:

@echo off
setlocal enabledelayedexpansion
for /D %%f in ("%cd%\*") do (
   set "name=%%~nf"
   set name=!name:LS_=!
   rename "%%~f" "SW_!name!"
   pause.
)

even better is to skip the second set replacement:

@echo off
setlocal enabledelayedexpansion
for /D %%f in ("%cd%\*") do (
   set "name=%%~nf"
   rename "%%~f" "!name:LS=SW!"
   pause.
)

Additionally. You are setting the name of the variable to the filename only, not the extension included, this will rename the file to exclude the extension, is this your intensions? If not, change %%~nf to %%~f

In order read more about delayedexpansion, see set /? and setlocal /? from cmd.exe.

Gerhard
  • 22,678
  • 7
  • 27
  • 43