none
Batch file for %% variable/rename %%~nf.* RRS feed

  • Question

  • Hi

    I have the batch script below which does the following:

    1. recursively extracts .zip files (each zip contains only 1 file) using 7zip to a temp location

    2. Each extracted file is renamed to the name of the .zip file preserving the extension - ren C:\DME\temp\*.* %%~nf.*

    3. The file is then moved to new location which shares the same structure except the parent folder is slightly different. 

    This all works fine except I have noticed one problem. 

    ren C:\DME\temp\*.* %%~nf.*

    This line renames the file to up to ".". If the file has a name with a number of "." I get a problem. Example = test.1.4.doc. My script renames this newfilename.1.4.doc instead of newfilename.doc

    Because my %% variables apply to the original loop command (I think) I cant use %%~xI for example as this would just return .zip. I have tried a nested loop but enabledelayedexpansion does not seem to be working as %%f = the original file path.

    Can anyone point me in the right direction?

    Thanks

    @echo off
    setlocal enabledelayedexpansion
    y:
    cd "Y:\Scriptingv1\filepath\DME\data\00"
    
    for /r %%f in (*.zip) Do ( 
    	"C:\Program Files\7-Zip\7z.exe" e -aoa -oc:\DME\temp "%%f" 1>>C:\DME\logs\7ziplog.txt 2>&1
    	
    	Set currentpath=%%~pf
    	Set newpath=!currentpath:Scriptingv1=Original!
    
    	ren C:\DME\temp\*.* %%~nf.*
    	move /Y C:\DME\temp\%%~nf.* "!newpath!"
    	
    	@echo on
    	echo Moving file to !newpath!
    )




    • Edited by Tee-Eff Thursday, May 8, 2014 2:38 PM
    Thursday, May 8, 2014 2:36 PM

Answers

  • Rather than trying to fix your shell script, I would recommend rewriting it in PowerShell. It will be easier to write and easier to read later.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Tee-Eff Thursday, May 8, 2014 6:07 PM
    Thursday, May 8, 2014 2:48 PM
    Moderator

All replies

  • Rather than trying to fix your shell script, I would recommend rewriting it in PowerShell. It will be easier to write and easier to read later.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Tee-Eff Thursday, May 8, 2014 6:07 PM
    Thursday, May 8, 2014 2:48 PM
    Moderator
  • Took your advice and have achieved what I wanted in a much easier way

    thanks

    Thursday, May 8, 2014 6:07 PM