Removing first Characters from a lot of files

July 23, 2011 at 23:24:42
Specs: Windows 7
I found a few threads like this, on this site, from google search but there is a problem. I don't know how to use scripts and I don't know how to a adapt one i found to my character number and file location.

Problem: I have just shy of a thousand files I am trying to sort out and remove duplicates but I cannot alphabetize them because they are all number is some random order, eg: "#### - filename.ext"

I want to remove the first seven characters from all the files. thank you for any help.


See More: Removing first Characters from a lot of files

Report •


#1
July 24, 2011 at 01:24:00
If there are files in the directory named:
1234 - Filename.txt
and
2345 - Filename.txt
removing the first 7 chars would mean that identical filenames (Filename.txt) would exist which is not permitted. How would you want that handled?


Please come back & tell us if your problem is resolved.


Report •

#2
July 24, 2011 at 23:59:07
There wouldn't be any exact duplicates as files are sorted by region. So there would be filename u.ext and filename e.ext but even if there were an exact, if that is a precaution that needs to be taken, then one just needs to be deleted. These aren't sensitive files so I won't miss one our two.

Report •

#3
July 25, 2011 at 01:29:35
Okay thanks. Test this, no files are deleted in the script, if a duplicate filename is created an error message will be displayed and we can adjust the script at that time. The %temp%\trial\ directory was created for testing purposes, you need to change the path to suit your system but because renaming is involved I recommend you set up a testing environment. Please post back with your result.

@echo off
cls
setlocal

dir %temp%\trial\

pushd %temp%\trial\||echo Pushd failed - job terminated.&&exit /b

for /f "tokens=*" %%1 in ('dir /a-d /b') do (
    for /f "tokens=1-2*" %%A in ("%%1") do (
        ren "%%1" "%%C"
  )
)
    
dir %temp%\trial\


Please come back & tell us if your problem is resolved.


Report •

Related Solutions

#4
July 25, 2011 at 01:42:06
Please tell me how to usethe script.

Report •

#5
July 25, 2011 at 21:10:46
OK - A testing environment was created using the following script. The %temp%\trial\ directory is created and files are written to that directory for testing purposes only. This script must be run prior to running the main script otherwise files will not exist.

@echo off
cls
setlocal

if not exist %temp%\trial\ md %temp%\trial\
echo y|del %temp%\trial\*.*

for /l %%1 in (1,1,20) do (
    echo >"%temp%\trial\%%1 - file name%%1.ext" ryryryr
)

dir %temp%\trial\

Here is the main script with some adjustments to let you see the results. The added commands can be deleted and paths amended if and when you are satisfied with the results. You will appreciate that you did not post the path to the files so the script must be amended by you.

@echo off
cls
setlocal


pushd %temp%\trial\||echo Pushd failed - job terminated.&&exit /b

echo.Input filenames are:
dir /b 

for /f "tokens=*" %%1 in ('dir /a-d /b') do (
    for /f "tokens=1-2*" %%A in ("%%1") do (
        ren "%%1" "%%C"
  )
)
echo.
echo Files renamed to:    
dir /b %temp%\trial\


Please come back & tell us if your problem is resolved.


Report •

#6
July 25, 2011 at 21:53:39
I don't think you understand. I dont know what to do with the script. I don't know how to run it.

Report •

#7
July 26, 2011 at 00:52:10
Sorry, I did not appreciate that you apparently do not know how the Command Shell operates otherwise I would not have posted the scripts. You should Google for Command Shell tutorials and study them before attempting to use any script which renames or deletes files.

Meantime if you wish to rename the files you should use the GUI.

Good luck.


Please come back & tell us if your problem is resolved.


Report •


Ask Question