1

I did batch file which copy 3 files and need to rename it by removing last 33 characters. The copy works fine but removing last 33 characters not... I saw more then one answer on web and try it all but nothing work so far.

My batch file look like this:

for /f "delims=" %%i in ("my folder")  do (
    ren "%%i" "%i:~0,-33%".txt
)

I tried already:

set fName=%%i
ren "%fName%" "%fName:~0,-33%.txt"
Thomas Bormans
  • 5,156
  • 6
  • 34
  • 51
Tzahi Kadosh
  • 397
  • 2
  • 9
  • 21
  • another error for trying : set FOLDER_PATH=XXXX for %%f in (%FOLDER_PATH%*) do if %%f neq %~nx0 ( set "filename=%%~nf" ren "%%f" "!filename:~0,-33!%%~xf" ) the system cannot find the file specified – Tzahi Kadosh Jun 07 '16 at 11:00
  • Do the `%fname%` one again, but use [delayed expansion](http://stackoverflow.com/questions/9681863/windows-batch-variables-wont-set) this time. – SomethingDark Jun 07 '16 at 11:35

3 Answers3

1

From the information I got here, try this:

@echo off
setlocal enabledelayedexpansion

set "folderpath=[Your Folder Here...]"
cd %folderpath%
for /f %%a in ('dir /b "*.txt"') do (
   set "fname=%%~na"
   ren "%%a" "!fname:~0,-33!.txt"
)
endlocal

This is similar to the answer above. You should make sure the batch file is OUTSIDE the folder.

EDIT. When dealing with variables formed inside FOR and IF's, use delayed expansion (i.e. !var!, instead of %var%). Anyway, this is the fixed code:

@echo off
setlocal enabledelayedexpansion

::NO Last Backslash...
set "sourcepath=C:\Users\tzahi.k\Desktop\testSource\source2"
set "folderpath=C:\Users\tzahi.k\Desktop\testSource\des"

for /F "delims=" %%a in ('dir /b /od "%sourcepath%\*.txt"') do (
   set "youngest=%%a"
   xcopy /y "%sourcepath%\!youngest!" "%folderpath%"
)

cd /d %folderpath%
for /f %%a in ('dir /b "*.txt"') do (
    set "fname=%%~na"
    ren "%%a" "!fname:~0,-33!.txt"
)
endlocal
pause
Poypoyan
  • 446
  • 6
  • 15
  • now i've got different problem...this code works fine alone but when i put it under my copy code its stop working look like this set folderpath=C:\Users\tzahi.k\Desktop\testSource\des for /F "delims=" %%a in ('dir /b /od "C:\Users\tzahi.k\Desktop\testSource\source2\*.txt"') do set Youngest=%%a xcopy /y "C:\Users\tzahi.k\Desktop\testSource\source2\%Youngest%" %folderpath% cd %folderpath% for /f %%a in ('dir /b "%folderpath%\*.txt"') do ( set "fname=%%~na" ren "%%a" "!fname:~0,-33!.txt" ) pause – Tzahi Kadosh Jun 08 '16 at 05:23
0

Here's the batch file you'd want to run:

@echo off
Setlocal EnableDelayedExpansion

@for /f "delims=" %%i in ('dir /b *.txt')  do (
    set fname=%%~ni
    set fname=!fname:~0,-33!.txt
    ren "%%i" "!fname!"
)

endlocal
Filipus
  • 520
  • 4
  • 12
  • thanks but now i've got another error says Access is denied... any suggestion? – Tzahi Kadosh Jun 07 '16 at 13:04
  • Hard to tell, but if you originally copied your files from a network or shared folder, the read-only attribute may have been set by the copy operation on the destination files. You could start the batch file with a **attrib -r *.txt** command just to make sure. – Filipus Jun 07 '16 at 18:36
  • What works only for the first file? The batch file or the Attrib command? What errors do you get after the first file? Still _Access Denied_? Where did you add the **attrib** command in the batch file? – Filipus Jun 08 '16 at 20:39
0

This should work

@echo off
setlocal enabledelayedexpansion
set FOLDER_PATH=C:\Some\Path\
for %%f in (%FOLDER_PATH%*) do if %%f neq %~nx0 (
    set "filename=%%~nf"
    ren "%%f" "!filename:~0,-33!%%~xf"
)
PAUSE

Or better this

@echo off & setLocal enableDELAYedeXpansion
for /f "tokens=* delims= " %%a in ('dir /b *.txt') do (
set F=%%~Na
set F=!F:~0,33!
move /y "%%a" "!F!%%~Xa"
)
Rishav
  • 3,818
  • 1
  • 31
  • 49