0

I cant understand how exactly we use reges in batch files.

I simple need to replace vm_[^_]*_ with nothing on ONLY .txt files in subfolders of lets say C:/ProjectFiles/

The file names look like:
vm_dsr_ads_fire.txt
vm_m1911_idle.txt

I need them to end up with the file names:
ads_fire.txt
idle.txt

How do I do this?

  • As I said pure batch has no regex capabiliies, you need either external tools or a different scripting language. For the renamingI can recommend jren.bat from dbenham which is a batch/jscript hybrid. You just have to check for the exact regex syntax – LotPings Nov 5 '16 at 13:17
  • @LotPings findstr has regex capabilities, just not replace. – barlop Nov 5 '16 at 14:37
  • sed(third party), and a "bouncing off the command prompt" technique would be simple and flexible.. moreso than a 'for'. – barlop Nov 5 '16 at 14:39
  • @barlop Thanks, I'm aware of the very limited RegEx capabilities of findstr. Nevertheless findstr is an indispensable tool. – LotPings Nov 5 '16 at 14:47
0

By reading the help for and exercising?

Lets get serious, you don't need a regex here (if so then you would be better off using vbscript or Powershell)

This batch generates a DoRename.cmd you may revise and execute afterwards. CHange Basefld to fit your env.

@Echo off
Set Basefld=Q:\Test
Pushd %Basefld% ||(Echo can't cd to %Basefld%&Pause&Exit /B 1)
> DoRename.cmd (
For /F "Delims=" %%A in ('Dir /B/S "vm_*_*.txt"'
  ) DO For /F "tokens=1,2* Delims=_" %%B in ('Echo:%%~nxA'
    ) Do Echo Ren "%%~A" "%%D"
)
start "" Notepad.exe DoRename.cmd

This second batch has no unneccessary seatbelts and will immediately rename files in current dir and subdirs.

@Echo off
For /F "Delims=" %%A in ('Dir /B/S "vm_*_*.txt"'
  ) DO For /F "tokens=1,2* Delims=_" %%B in ('Echo:%%~nxA'
    ) Do Ren "%%~A" "%%D"

The first for walks the tree for fhe files, the second parses the filename and extension. delims=_ splits the name into tokens. token1 aka %%B being ´vm´ token2 %%C the text between the first 2 underscores and token3 * %%D the rest of the name including the .ext .

  • 1
    Please get rid of all of these "Set" and checks to see if they can CD - They arent needed for me - A simple CD Q:\Test will suffice. – user657868 Nov 5 '16 at 11:23
  • Im a bit confused what its actually doing doe. I simply want code to do regex. – user657868 Nov 5 '16 at 11:25
  • Output: Ren "C:\Program Files (x86)\Steam\steamapps\common\Call of Duty Black Ops III\xanim_export\custom\weapons\an94\vm_an94_ads_down.XANIM_BIN" "export\custom\weapons\an94\vm") It doesnt actually rename . – user657868 Nov 5 '16 at 11:30
  • Sorry for that. Edited the batch, try again. The first for walks the tree for fhe files, the second parses the (now) only the filename and extension. delims=_ splits the name into tokens. token1 aka %%B being ´vm´ token2 %%C the text between the first 2 underscores and %%D the rest of the name. I have to correct the batch for this. 1minute – LotPings Nov 5 '16 at 11:38
  • 1
    The file mask should include two underscores as vm_*_*.txt so as not to incorrectly rename files that don't meet the correct pattern. – dbenham Mar 23 '19 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.