0

I'm looking to make a batch file that will remove specific prefixes from files within a folder.

eg. Rename "1 File1.txt" and "1 File2.txt" to File1.txt & File2.txt respectively. The problem is that that the length of the prefix can vary. Here is the code I have:

@echo off
Set /p Prefix = Enter the prefix to eliminate:
rename "%Prefix%*.txt" "//*.txt"

Is there a way to make the amount of slashes (characters to remove) equivalent to the amount of characters entered by the user? Or another way to achieve the same thing?

Edit: Accidentally listed the files as "1 File1.txt and 2 File2.txt" - this was meant to be "1 File1.txt and 1 File2.txt" - all files with the same prefix (in this case, the prefix simply being "1 "

Justin Lund
  • 181
  • 1
  • 1
  • 10
  • 1
    is there a common delimeter in your filename? like a space? like your example `1 file1.txt` – Gerhard Jan 28 '19 at 06:54
  • Open a command prompt window and run `ren /?` or `rename /?` to get output the help of this command. The first argument string can be with path, but the second argument string being the new name must be without path. `/` is the directory separator on Unix/Linux/Mac while on Windows the forward slash is usually used for options. For compatibility reasons Windows kernel replaces all `/` by the Windows directory separator ``\`` in a file/folder string before accessing the file system. So it is not possible to use `/` in name of a file or folder. – Mofi Jan 28 '19 at 07:23
  • 1
    See also the Microsoft article [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). It looks like what you want is removing everything left to first sequence of one or more spaces in the file names. This can be achieved with `for /F "eol=| delims=" %%A in ('dir "* *.txt" /A-D-H /B 2^>nul') do for /F "eol=| tokens=1*" %%B in ("%%A") do ren "%%A" "%%C"`. – Mofi Jan 28 '19 at 07:30
  • I recommend reading [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) You do not define an environment variable with name `Prefix`. You define an environment variable with name `Prefix ` with a space appended to name. Never use spaces left to equal sign on argument string of command `set`. And right to equal sign a space character is often also wrong, but in some cases intentionally used. – Mofi Jan 28 '19 at 07:35
  • This question sounds a bit like an [XY Problem](http://xyproblem.info) to me... – aschipfl Jan 28 '19 at 12:24
  • @GerhardBarnard Yes, space is a common delimiter – Justin Lund Jan 29 '19 at 08:07
  • So, did you look at the answer I provided? – Gerhard Jan 29 '19 at 08:15
  • @GerhardBarnard The solution you provided didn't seem to work, however the one from Mofi did I appreciate the input and help from both of you! – Justin Lund Jan 29 '19 at 08:19

2 Answers2

0

If you want to remove the prefix automatically, using your example files 1 file1.txt 2 file2txt:

@echo off
set /p "input=Enter Prefix to search: "
for /f %%a in ('dir /b /a-d %input%*.txt') do (
   for /f "tokens=1,*" %%i in (%%a) do if not "%%i"=="" if not "%%j"=="" echo move "%%a" "%%j"
)

It will prompt the user for a prefix to search, i.e 1 and then do a search for anything containing 1*.txt and rename it without the first delimeter. So even if the user adds 255 as prefix, it will search for anything 255*.txt (Note there are no spaces before or after my = in my set command.

This example will simply echo the move command and not actually move the files, if you want to perform the actual task, remove the echo command from the last for loop.

Keep in mind, if a file is called 1 and file1.txt it will rename the file to and file1.txt because of tokens=1,* Also, we are only running this for files containing whitespace as per your examples, not _ - etc, but can be changed if need be.

Lastly, you can also automatically rename all files, without having a user prompted. i.e

@echo off
for /f %%a in ('dir /b /a-d *.txt') do (
   for /f "tokens=1,*" %%i in (%%a) do if not "%%i"=="" if not "%%j"=="" echo move "%%a" "%%j"
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

The answer provided by Mofi in the comments is exactly what I'm looking for, thank you! The common delimeter is a space, so the code works great.

for /F "eol=| delims=" %%A in ('dir "* *.txt" /A-D-H /B 2^>nul') do for /F "eol=| tokens=1*" %%B in ("%%A") do ren "%%A" "%%C"
Justin Lund
  • 181
  • 1
  • 1
  • 10