Jump to content
LaptopVideo2Go Forums

Batch file to remove a '.' (dot) from a file name


mobilenvidia

Recommended Posts

With my rewriting of the driver squashing app, I have a small issue.

The files names once squashed become ie 290.36_win7x64.exe I would like the first '.' removed so it reads 29036_win7x64.exe

ie all the drivers are xxx.yy_winversion.exe so the 4th character from left needs to be removed if this makes it easier

Is there a DOS guru that can pass on code that would do that in a batch file ?

I will be for ever indebted and you will have a life time of free INF mods :)

Link to comment
Share on other sites

I don't know about DOS, but you can do it with your eyes closed using Python. Just install any 3.x version, paste the code below into a .py file (or just download the attached script), place the script inside the folder with your .exe files, double-click it and it'll do all the work.

import sys
import os
def lv2grename():
print("Renaming files...")
for filename in os.listdir('.'):
	sp=filename.split(".")
	if len(sp)>2 and sp[2]=='exe':
		new=sp[0]+sp[1]+".exe"
		print("\t" + filename + " -> " + new)
		os.rename(filename, new)

if __name__ == '__main__':
lv2grename()

EDIT: Whoops something went wrong while attaching the file, seems like forums don't like .py files :)

lv2g_renamescript.7z

Edited by dark_skeleton
Link to comment
Share on other sites

I made this code in Windows 8 but I don't know if the bug is from Windows or the FOR implementation or the function is intended to have such weird bahavior. I think that as the name changed it assumed that there was a new file and made a 2nd iteration on the same file but I don't know for sure because it only reprocesses the 1st file making extra undesired letter removal. Very very weird indeed.

@echo off

for %%f in (*.exe) do call:renamer %%f
goto:done

:renamer
set filename=%1
ren %1 %filename:~0,3%%filename:~4%

:done

So as a workaround I made a change assuming that the file to be renamed has 2 dots in the name so that after removing 1 dot it won't delete anything else.

@echo off

for %%f in (*.*.exe) do call:renamer %%f
goto:done

:renamer
set filename=%1
ren %1 %filename:~0,3%%filename:~4%

:done

I sincerely hope that this is what you're needing. If it's not then explain better your intention please :)

Edited by Beyond
Link to comment
Share on other sites

Use total commander's multirename feature. It's pretty easy to figure out and you're done in less than 5 minutes without installing any runtimes or writing scripts.

You have to install that app too :)

Link to comment
Share on other sites

This will indeed remove all dots from filenames, leaving extensions intact.

Also it's possible to enable recursion (to enable dropping of directories), just add /R to 1st FOR before %%I.

RemoveDotsFromFileName.cmd

@REM coding:OEM
@ECHO OFF
SETLOCAL

IF "%~1"=="" GOTO :help
:again
FOR %%I IN (%1) DO CALL :RenameSingle "%%~I" "%%~nI" "%%~xI"

SHIFT
IF NOT "%~1"=="" GOTO :again

EXIT /B

:RenameSingle
REM %1 is renamed file
REM %2 is filename w/o ext
REM %3 is extension
SET NoDotsLast=%~2
:CheckDots
CALL :LeaveNoDots "%NoDotsLast%" || GOTO :CheckDots
REN %1 "%NoDotsLast%%~3"
EXIT /B

:LeaveNoDots
SET NoDotsName=%~n1
SET NoDotsExt=%~x1
rem check for dots in %NoDotsLast%
IF NOT DEFINED NoDotsExt EXIT /B 0

REM %~1 means from 1st char till the end
SET NoDotsLast=%NoDotsName%%NoDotsExt:~1%
EXIT /B 1

:help
ECHO Drop files to this batch, or create shortcut to one and drop files on the shortcut.
ECHO Any key to exit for now.
PAUSE >NUL

Edited by LogicDaemon
Link to comment
Share on other sites

Cheers guys, I'll have a look at this as soon as it stops raining drivers

But then again I need it to process drivers, catch 22

I need it to drop into my current Batch file that crunches the the drivers.

When I run batch file I type in the number of the driver ie '295.18'

So I have the variable %NV_DV% with 298.15

The batch file then looks for all 4 versions of the OS possiblities

295.18 (being winxp)

295.18_winxp64

295.18_win7x32

295.18_win7x64

I just add the OS version to the end still leaving %NV_DV% at 298.15

Now once the driver is crunched I need %NV_DV% to become 29815.exe (loosing dot)

This is for the next process in a completely different app.

Link to comment
Share on other sites

hahaha after all you just need to edit 1 variable in your batch. Should have guessed. In that case the only thing you need to do is add this line in your code: set NV_DV=%NV_DV:~0,3%%NV_DV:~4%

Enjoy :)

Link to comment
Share on other sites

hahaha after all you just need to edit 1 variable in your batch. Should have guessed. In that case the only thing you need to do is add this line in your code: set NV_DV=%NV_DV:~0,3%%NV_DV:~4%

Enjoy :)

Free modded drivers for life :clap:

Made a slight mode to the above, as I need the dot for the other versions of the compress process.

So made another variable with no dot that 7zip can use.

Woohoo, I'm fairly confident in driver modding but programming not so.

Been a long time since I wrote machine code for the MOS6502 and my Commodore Pet.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...