Rename File Extensions Only, Keep Filenames

Hi. I’m trying to rename the file extension only and keep the original filename(s) of “anyfile.txt” of specific directories. For example…

All .txt files in a (Sample) folder…
C:\Sample\anyfile001.txt
C:\Sample\anyfile002.txt
C:\Sample\anyfile003.txt
C:\Sample\anyfile004.txt

…get renamed to an .old file
C:\Sample\anyfile001.old
C:\Sample\anyfile002.old
C:\Sample\anyfile003.old
C:\Sample\anyfile004.old

The plain commands like ren *.txt *.old or forfiles /S /M *.txt /C “cmd /c rename @file @fname.old seem to plainly work when run locally, like in a command prompt or batch file (run within the directory), but run them through the FixletDebugger or BigFix Action and it’s all Exit Code=1 (and/or nothing happens).

The same goes for PowerShell; works when run in PowerShell ISE, but “Exit Code=1” in FixletDebugger:
Get-ChildItem -Path “C:\Sample*.txt” | ForEach-Object {move-item $.FullName -Destination "$($.DirectoryName)$($_.BaseName).old" -Force}

I guess I’m more interested in why these don’t work when run through FixletDebugger (and if there’s a workaround), but I’d welcome any other clever solution(s). Thanks.

What is the code you are running in the debugger?

This works for me

waithidden cmd /c "ren c:\test\*.txt *.old"

 Directory of c:\test

22/06/2021  15:05    <DIR>          .
22/06/2021  15:05    <DIR>          ..
22/06/2021  15:04                 5 ABC.txt
22/06/2021  15:04                 5 DEF.txt
               2 File(s)             10 bytes

becomes

 Directory of c:\test

22/06/2021  15:06    <DIR>          .
22/06/2021  15:06    <DIR>          ..
22/06/2021  15:04                 5 ABC.old
22/06/2021  15:04                 5 DEF.old
               2 File(s)             10 bytes
4 Likes

Unfortunately, it wasn’t that simple because of unknown directory names, but I think I might have figured it out…here’s what I have and it seems to work.

// Change .txt to .old file for C:\Program Files\SAMPLE\AnyName\Sample1

if {(exists files whose (name of it as string as lowercase ends with “.txt”) of folders ((it & “\Sample1”) of pathnames of folders of folders (value of variable “ProgramW6432” of environment & “\SAMPLE”)))}
appendfile {concatenation “%0d%0a” of (“cd %22” & it & “\Sample1” & “%22 && forfiles /S /M *.txt /C %22cmd /c rename @file @fname.old%22”) of pathnames of folders whose (name of it starts with “Any” of folders ((value of variable “ProgramW6432” of environment) & “\SAMPLE”)}
endif
copy __appendfile sample.bat
wait cmd /C sample.bat

When the sample.bat is run, cmd prompt takes 2 actions:

  1. cd “C:\Program Files\SAMPLE\AnyName\Sample1”
  2. forfiles /S /M *.txt /C “cmd /c rename @file @fname.old

That “AnyName” can be anything–a different version no., in my case, which is why it’s set up that way and able to apply to multiple folders.

1 Like

UPDATE: I got the original “ren” working…

After the appendfile {concatenation “%0d%0a” of …

(“ren %22” & it & “\Sample1” & “*.txt%22 *.old”) of pathnames of folders whose (name of it starts with “Any” of folders ((value of variable “ProgramW6432” of environment) & “\SAMPLE”)

The appendfile sample.bat will contain:
ren “C:\Program Files\SAMPLE\AnyName\Sample1*.txt” *.old

The original problem I was having turned out to be the placement of the quotation marks (the “%22”) within the bat file. I had run things with the quotation mark before the ren or after the *.old, resulting in errors (which appears to work in the fixletdebugger with the waithidden cmd /c, but not run as a batch file).

2 Likes