-1

I have a lot of files named "1.json", "2.json', "3.json" and so on I need a script to add "#" symbol before each filename

I'm very new to batch scripts, I only just find someone's scripts and try them without understanding how it really works I found this one:

@echo off    
SETLOCAL ENABLEDELAYEDEXPANSION    
SET old=5
SET new=#5 
for /f "tokens=*" %%f in ('dir /b *.json') do (    
    SET newname=%%f    
    SET newname=!newname:%old%=%new%!    
move "%%f" "!newname!"    
)

But I don't understand how to use it when all my filenames are different and don't have any similar words

Compo
  • 36,585
  • 5
  • 27
  • 39
Pavel
  • 1
  • `move "%%f" "#%%f"` – Stephan Sep 01 '23 at 18:56
  • Using your own `for /f` methodology, but as a one line batch file: ```@For /F "EOL=? Delims=" %%G In ('Dir "*.json" /A:-D /B 2^>NUL ^| %SystemRoot%\System32\findstr.exe /BLV "#"') Do @Ren "%%G" "#%%G"```. – Compo Sep 01 '23 at 18:56
  • This script worked, just edited the old one a bit, thanks to @Stephan `@echo off SETLOCAL ENABLEDELAYEDEXPANSION for /f "tokens=*" %%f in ('dir /b *.json') do ( SET newname=%%f SET newname=!newname! move "%%f" "#%%f" )` – Pavel Sep 01 '23 at 19:27
  • From the comment above, you should note, there is no need for `SETLOCAL ENABLEDELAYEDEXPANSION`, `SET newname=%%f` or `SET newname=!newname!`. You should also note that my previous comment includes the additional feature of not renaming any JSON which is already prepended with a `#` character. – Compo Sep 01 '23 at 20:10
  • Oh, yes, thank you, I removed that and it's still working. As I said, I don't understand how it all works I was a bit afraid using your code when I saw %SystemRoot%\System32\findstr.exe /BLV, to be honest, because I have no idea what that is – Pavel Sep 01 '23 at 20:47
  • 1
    **1.** You could have asked. **2.** You could have researched it. **3.** You could have tried it with a test directory. – Compo Sep 02 '23 at 00:59

0 Answers0