Jump to content

[Batch] Remove a specific part of a filename


Recommended Posts

HI,
I'm suck at the last part of my script and I'm try to remove a specific part of a filename. Like:

From ->
Some Image File 1 SHA-1; 843j0b5a5a06ff252cab25d2142beva7a072aea3.jpg
Some Image File 2 SHA-1; fg3j0b5a5df3ff252cab25ddb242bev26a072zsdf.jpg
To ->
Some Image File 1.jpg
Some Image File 2.jpg

In Google i found a lot of results but nothing that would work for what i was trying to do. :} If this even works in Batch. :dubbio:

Edited by Mike86
Link to comment
Share on other sites


READ (attentively) this:

https://www.robvanderwoude.com/ntfortokens.php

then, it is more or less:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET LongName=Some Image File 1 SHA-1; 843j0b5a5a06ff252cab25d2142beva7a072aea3.jpg
FOR /F "tokens=1,3 delims=;." %%A IN ("%LongName%") DO (
SET name=%%A
SET ext=%%B
SET name=!name:~0,-6!
SET newname=!name!.!ext!
)
SET newname

jaclaz

Link to comment
Share on other sites

32 minutes ago, Mike86 said:

What if some file names have more then 6 spaces in the name?

Look, the 

SET name=!name:~0,-6!

removes LAST 6 characters, it has nothing to do with spaces.

You provided an example, and I proposed a possible solution for that example, in which the code removes " SHA-1", i.e. 6 characters before the semi-colon.

You can have the same result using (say):

FOR /F "tokens=1,2,3,4,7 delims= ." %%A IN ("%LongName%") DO ( 

SET name=%%A %%B %%C %%D 

SET ext=%%E 

SET newname=!name!.!ext!

)

But that fixes the name as having 4 tokens space delimited, i.e.
Some Image File #

Whilst the earlier suggestion would cover *anything* that is suffixed by "<6 characters>;<any number of characters>" I posted it explicitely to show you how to use (besides the final dot separating the extension, which can be obtained by other means, using variable expansion, see below) the semicolon as a separator and proceed from that, assuming that the " SHA1;" before is a fixed suffix.

For all it matters (again following your example) the suffix is always a fixed length, as the SHA-1 hash has a fixed length so you can also do:

CALL :shortname "Some Image File 1 SHA-1; 843j0b5a5a06ff252cab25d2142beva7a072aea3.jpg"
SET shortname

GOTO :EOF

:shortname
SET shortname=%~n1
SET shortname=%shortname:~0,-48%
SET shortname=%shortname%%~x1%

jaclaz

Link to comment
Share on other sites

The problem is that the file names are not always the same so it has to check if the SHA-1; exists in the file name. Like:
ABC 123 ABC.jpg
ABC 123 SHA-1; ks8j0b5a5df56252cab25ddb242bev26a077834b.jpg
ABC 123 ABC 123.gif
ABC 123 123 ABC SHA-1; fg3j0b5a5df3ff252cab25ddb242bev26a072zsdf.jpg

Edited by Mike86
Link to comment
Share on other sites

First search for the text "SHA-1"; in the file name and separate

@Echo Off

:: Create empty files to contain file names parsed yes / no shafiles
Echo. 2>yshafiles.txt
Echo. 2>nshafiles.txt

:: For all jpg and gif files in actual folder
For %%a In (*.jpg *.gif) Do (
  :: Search "SHA-1;" in file name and save
  Echo %%a | Find "SHA-1" && Echo %%a >> yshafiles.txt || Echo %%a >> nshafiles.txt
)

After use @jaclaz implementation

Edited by EdSon
Link to comment
Share on other sites

Is this the best way to do it?

@ECHO OFF

For %%a In ("*SHA-1;*.jpg" "*SHA-1;*.gif") DO (CALL :shortname "%%a")

:shortname
SET shortname=%~n1
SET shortname=%shortname:~0,-48%
SET shortname=%shortname%%~x1%

ECHO %shortname%
pause

The only problem is that i still get an out put ~0,-48 if nothing was found.

Edited by Mike86
Link to comment
Share on other sites

After (CALL :shortname "%%a") the next line to run is :shortname ..., this set shortanme variable to ~0,-48. Use to GoTo label for jump:

@Echo Off

For %%a In ("*SHA-1*.jpg" "*SHA-1*.gif") Do Call :shortname "%%a"
Goto :continue

:shortname
  Set shortname=%~n1
  Set shortname=%shortname:~0,-48%
  Set shortname=%shortname%%~x1%
  Echo %shortname%
Goto :EOF

:continue
  Pause
Goto :EOF

 

Link to comment
Share on other sites

Well, if you post only examples that do contain " SHA-1;" isn't it surprising that proposed solutions take care of that only?

Can we assume now that the new conditions are:

1) filenames may (or may not) contain the suffix " SHA-1;<sha-1_hash>
2) filenames (before and besides the suffix) may (or may not) contain spaces

In case of the file name with the suffix it must be stripped, in case the file name without the suffix the file name should remain "as is".

SET ShortName=
ECHO "%LongName%"|FIND " SHA-1;">NUL&&CALL :shortname "%LongName%"
IF NOT DEFINED ShortName SET ShortName=%LongName%
SET shortname

jaclaz

 

Link to comment
Share on other sites

This simple script is the VBS way of renaming a file using Drag And Drop and  changing the file name to

one with using HourMinuteSecond.FileExtension for it new name 

'-> Object To Copy And Delete File 
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
'-> Run Time Varibles
Dim FName, Nname 
'-> Check For Drag And Drop And Then Process A Single File
 If WScript.Arguments.Count = 1 Then
'-> Get File So You Can Get It Path
  Set FName = Fso.GetFile(WScript.Arguments(0))
'-> File New Name Using HourMinutesSecomds.FileExtension
  Nname = Hour(Now) & Minute(Now) & Second(Now) & "." & Right(FName.Path,3)
'-> Copy Drag And Drop File To It New Name
    FName.Copy(Replace(FName.Path,FName.Name,Nname)),True
'-> Delete The Original Faile 
    Fso.DeleteFile WScript.Arguments(0),True
 Else 
'-> For No File Or To Many File Error 
  If WScript.Arguments.Count = 0 Then WScript.Echo _
   "Drag and Drop One File On To This Script To Have It Function"
  If WScript.Arguments.Count > 1 Then WScript.Echo _ 
   "To Many Files, This Script Can Process Only 1 File at a Time"
 End If 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...