0

I need to rename lots of files using command prompt, I want to use command promt please (casue I cant install third party software or python etc)

I tried to write some command prompt script, it seems to be working but just missing the finishing touch. the problem is that my script somehow rename all the files with the last folder name, not iterative

for /d %i in ("C:\Users\noir1\Desktop\New folder\*") do ( cd "%i" & set _test=%i & for %a in (*) do ren "%a" "%_test:~34,13% %a")

what I have: within "New folder" there are 4 more folders:

  1. "New folder 1xxxx" and within this folder I have "file1, "file2", "file3"
  2. "New folder 2xxxx" and within this folder I have "file1, "file2", "file3"
  3. "New folder 3xxxx" and within this folder I have "file1, "file2", "file3"
  4. "New folder 4xxxx" and within this folder I have "file1, "file2", "file3"

I want to see file1, file2, file 3 name within all the folders to change base on the parent folder:

  1. "New folder 1xxxx" and within this folder I have "New folder 1 file1", " New folder 1 file2", "New folder 1 file3"
  2. "New folder 2xxxx" and within this folder I have "New folder 2 file1", " New folder 2 file2", "New folder 2 file3" and so on

But instead if I use my cript above I got

  1. "New folder 1xxxx" and within this folder I have "New folder 4 file1", " New folder 4 file2", "New folder 4 file3"
  2. "New folder 2xxxx" and within this folder I have "New folder 4 file1", " New folder 4 file2", "New folder 4 file3"
DSt_FTW
  • 19
  • 6
  • 1
    You have a [delayed expansion problem](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028). Do you have a requirement to do it directly on command line instead of using a batch file? – Stephan Jul 19 '19 at 09:08
  • @Stephan hi I am comnplete newb trying to dig myself into coding, how to batch file? – DSt_FTW Jul 19 '19 at 09:11
  • let me show you in an answer. Give me some minutes – Stephan Jul 19 '19 at 09:14

1 Answers1

1

The cmd language is scriptable (it's called a batch script or batch file). Just put the commands into a text file with extension .bat). You then can execute the whole script by just typing its name.

A batch script version of your command line could look like:

@echo off
setlocal enabledelayedexpansion
for /d %%i in ("C:\Users\noir1\Desktop\New folder\*") do (
  cd "%%i" 
  set "_test=%%i"
  for %%a in (*) do ECHO ren "%%a" "!_test:~34,13! %%a"
)

Note: the syntax for use in batch file may differ slightly for some commands (especially for uses %a type variables on command line, while in batch files you have to use %%a).

Note: I disabled the ren command by just echoing it. If you are satisfied with the result, just remove the ECHO to enable the ren command.

Advantages: no need to type the same code each time you want to do something and so avoid typing errors
readable code by formatting
reusable
large scripts with a lot of commands possible.

I recommend to bookmark SS64 and visit it regularly.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • excellent manage to make it work, yeah batching seems to be more sustainable, tahnk you very much – DSt_FTW Jul 19 '19 at 09:35