11

I have a folder with 120 .cs files.

What I need to do is add "DO" to every files name, before the extension.

Heres what i came up with:

ren *.cs *. //this removes all file extensions
ren *.* *DO.cs // this should rename all the files and add the suffixes

Now, this works mostly. But not for some Files.

Initially, I thought that it was just the files starting with Do

But... well, here is how it turned out:

folder-view

(The rest thats not on the printscreen is fine)

Now this aint a serious problem; id just like to understand why it did this...

There is my cmd output: cmd output

From looking at this output i thought maybe it cant find the files because the second command consists of . for the renaming; but since none of the files in the folder has a file extension at the moment of renaming; this cant be it..

Thanks

4 Answers4

10

As others told, it lies in the way ren interprets wildcards, that's why it can't find files that contains dots *.* in second command, because:

  • You not only removed file extension using first command, but also dots before the extensions:

Artikelnummer.cs > Artikelnummer

  • And your second command is looking for filenames containing . (which there's none!):

ren *.*

  • Also consider ren uses * to refer to file name, so when you look for *.* (any name, any extension) you're using * to refer to filename and extension at the same time, which is confusing to ren command!

So, the conclusion is, the only problem was *.*, replace it with *:

ren *.cs *.DO.cs
ren * *DO.cs

However, if you want to rename by running a single line of code:

From a command prompt run:

for /f "tokens=* delims=" %a in ('dir /b "%FilesLocation%"') do if %~xa EQU .cs ren "%a" "%~naDO.cs"

Or save and run this script:

@echo off
for /f "tokens=* delims=" %%a in ('dir /b "%FilesLocation%"') do if %%~xa EQU .cs ren "%%a" "%%~naDO.cs"

And don't forget to change %FilesLocation% with it's real value.

4

I was looking for a way to add suffix "_t" to my images.

Just one simple PowerShell command did it !

CD "target folder path here"
Dir | Rename-Item -NewName { $_.basename + "_Suffix” + $_.extension}

enter image description here

zennni
  • 151
0

What you need is the for command. Off the top of my head, it goes something like this:

for /f "tokens=1,2* delims=." %a in ('dir *.cs /b') do (ren %a.%b %aDO.%b)

This does not work for filenames that contain more than one point though.

jon_two
  • 321
0

FOR has a mode to loop through files (also recursively), no dir /b is required (https://ss64.com/nt/for2.html). You need to pass a file glob or a list (*.cs, \path\*.txt,...)

Command line

FOR %_ IN (*.cs) DO @REN "%_" "%~n_DO.*"

In a batch file (use %%)

FOR %%_ IN (*.cs) DO @REN "%%_" "%%~n_DO.*"

Notes:

  • %_ is a valid variable

  • @ is to not dump rename commands for each filename

  • %~n_ returns only the name portion from %_ (removes the last extension)

  • Add /R "c:\directory" to recursively parse a directory (https://ss64.com/nt/for_r.html). Use with caution - there is no undo

  • Double quotes " are important because paths and filenames may include spaces

  • Won't work as expected with multiple extensions because in Windows only the last token is considered an extension. I.e. test.tar.gz will become test.tarDO.gz

I suggest you first try with FOR %_ IN (*.cs) DO echo REN "%_" "%~n_DO.*" for dry-run

venimus
  • 2,488