0

I'm new to batch scripting and cannot figure out why this isn't working since I took it almost verbatim from another post that says it works. Except in the Post they're replacing underscores "_" with a space " ".

My goal is to replace any underscores "_" in a filename with a dash "-".

@echo off
Setlocal enabledelayedexpansion

SET SRC_FOLDER=C:\test_src
SET EXT=txt

for %%a in (%SRC_FOLDER%\*_*.%EXT%) do (
    set "filename=%%a"
    set "!filename!" "!filename:_=-!"
    echo Filename = !filename!
)

I get the following errors:

C:\Scripts>test_script.bat
Environment variable C:\test_src\Underscore_1.txt" not defined
Filename = C:\test_src\Underscore_1.txt
Environment variable C:\test_src\Underscore_2.txt" not defined
Filename = C:\test_src\Underscore_2.txt

Any idea what is wrong with that set command. I usually use Bash scripting, but have no choice in this case but to use Batch, so I'm not sure what's wrong here...

----EDIT----

I also added another FOR loop, which I'm sure could be done within just one loop, but since I'm not familiar with the complexities of batch, I did it in its own. In the second loop I'm renaming ALL files in the directory by adding a Prefix to each filename, adding a timestamp. But, it appears that when it renames the files, one of the files is getting the Prefix added twice. Any idea why this is?

Here is the full code and the output:

@echo off
Setlocal enabledelayedexpansion

SET "SRC_FOLDER=C:\test_src"
SET "EXT=txt"
SET "TIMESTAMP=Scan-%date:~4,2%%date:~7,2%%date:~10,4%-%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%"

REM echo %TIMESTAMP%

for %%a in ("%SRC_FOLDER%\*_*.%EXT%") do (
    Set "filename=%%~na"
    Ren "%%a" "!filename:_=-!%%~xa"
)

for %%f in ("%SRC_FOLDER%\*.%EXT%") do (
    set "newfilename=%TIMESTAMP%-%%~nf%%~xf"
    echo New Name = !newfilename!
    Ren "%%f" "!newfilename!"
)

The strange thing is, it's only doing this to the first file it renames. If I rename the first file that it's doing this to so that is no longer the first file, it then does it to the next. i.e. renaming File1.txt to File5.txt, it then renames File2.txt twice. Am I missing something?

OUTPUT:

C:\Scripts>test_script.bat
New Name = Scan-10182019-13552864-File1.txt
New Name = Scan-10182019-13552864-File2.txt
New Name = Scan-10182019-13552864-File3.txt
New Name = Scan-10182019-13552864-File4.txt
New Name = Scan-10182019-13552864-Scan-10182019-13552864-File1.txt
New Name = Scan-10182019-13552864-Under-score-2.txt
New Name = Scan-10182019-13552864-Underscore-1.txt
Matt
  • 11
  • 3
  • Take a look at the [`set` command](https://ss64.com/nt/set.html), `set "!filename!" "!filename:_=-!"` is not the correct syntax (where did you copy that from?)... – aschipfl Oct 16 '19 at 20:38
  • aschipfl, I found that syntax here shown under the 1st "Answer", within the FOR loop: https://stackoverflow.com/questions/9383032/rename-all-files-in-a-directory-with-a-windows-batch-script – Matt Oct 18 '19 at 17:29
  • You seem tohave confused the commands `ren` and `set` when copying the code... – aschipfl Oct 18 '19 at 18:16

1 Answers1

1

Looks like just some problems with your double quotes.

@Echo Off
SetLocal EnableDelayedExpansion

Set "SRC_FOLDER=C:\test_src"
Set "EXT=txt"

For %%a In ("%SRC_FOLDER%\*_*.%EXT%") Do (
    Set "filename=%%~na"
    Ren "%%a" "!filename:_=-!%%~xa"
)
Compo
  • 36,585
  • 5
  • 27
  • 39
avery_larry
  • 2,069
  • 1
  • 5
  • 17
  • Cool, thank you. Now, that does appear to replace underscores with dashes. But, it appears to do it with the Full path as well, i.e. C:\test_src becomes C:\test-src... How can we apply this to only the filename? – Matt Oct 16 '19 at 19:37
  • 1
    Like this perhaps: set "filename=%%~nxa" ? – Matt Oct 16 '19 at 19:40
  • 1
    For safety, `SET "SRC_FOLDER=C:\test_src"`, `SET "EXT=txt"` and `("%SRC_FOLDER%\*_*.%EXT%")`. Then use `Set "filename=%%~na"` and `Ren "%%a" "!filename:_=-!%%~xa"`. – Compo Oct 16 '19 at 20:18
  • Thanks. I'll give that a try shortly and comment back. Much appreciated guys! – Matt Oct 16 '19 at 20:30
  • Thanks Guys, applied avery_larry's answer with Compo's comment and it works perfectly! – Matt Oct 16 '19 at 21:28
  • avery_larry, I have rolled back your edit, because it was not required, the code was correct as per my rewrite. – Compo Oct 17 '19 at 17:15
  • I just added an EDIT to my original post. I'm seeing something strange happen with the file rename. On a second loop. – Matt Oct 18 '19 at 17:58
  • Figured out my 2nd issue. Appears the FOR loop was the issue, something about it re-updating the list of filenames while processing through the for loop. So it renamed File1.txt and then while the for loop is processing it added the new filename that was just renamed to the list to be looped over again. I fixed it by using this instead: `for /f "delims=" %%f in ('dir /b /a-d %SRC_FOLDER%\*.%EXT%') do (` and then during the ren command I just had to include the `%SRC_FOLDER%` variable since the script isn't local to the directory and the path wasn't already included. – Matt Oct 18 '19 at 18:39