Solved

Stripping a filename upto a certain subfolder in batch

Posted on 2014-03-19
3
429 Views
Last Modified: 2014-03-22
Hi,

  I have a input file containing the format:

"X:\subX\file1.pdf"               ,"newfile1.pdf",<some string>,<some string>
"X:\subX\keepsub\file2.pdf","newfile2.pdf", <some string>, <some string>

I want to write a batch that strips the file path of X, keeping the keepsub subfolder when it occurs and replace it with Z:\file1.pdf and Z:\keepsub\file2.pdf respectively. The keepsub when it exists is the lowest depth subfolder.

I have something like the below but it only replaces X: with Z: and strips all the subfolders including the keepsub.

@echo off
setlocal
set SourceFile=input.txt
set TargetFile=input_new.txt
set NewFolder=F:\Data


if exist "%TargetFile%" del "%TargetFile%"

echo Processing '%SourceFile%' ...

for /f "tokens=1* delims=," %%a in ('type "%SourceFile%"') do (
      >>"%TargetFile%" echo "%NewFolder%\%%~nxa",%%b
)
echo ... done.
0
Comment
Question by:LuckyLucks
3 Comments
 

Author Comment

by:LuckyLucks
ID: 39940785
Correction:

I want to write a batch that strips the file path of X, keeping the keepsub subfolder when it occurs and replace it with Z:\Data\file1.pdf and Z:\Data\keepsub\file2.pdf respectively.

also
:

set NewFolder=Z:\Data (not F:)
0
 
LVL 83

Expert Comment

by:oBdA
ID: 39941067
Try this:
@echo off
setlocal enabledelayedexpansion
set SourceFile=input.txt
set TargetFile=input_new.txt
set NewFolder=Z:\Data
set KeepFolder=Keepsub

if exist "%TargetFile%" del "%TargetFile%"
echo Processing '%SourceFile%' ...
for /f "tokens=1* delims=," %%a in ('type "%SourceFile%"') do (
	set Directory=%%~dpa
	for %%d in ("!Directory:~0,-1!") do set Parent=%%~nxd
	if /i "!Parent!"=="%KeepFolder%" (
		set TargetFolder=%NewFolder%\%KeepFolder%
	) else (
		set TargetFolder=%NewFolder%
	)
	>>"%TargetFile%" echo "!TargetFolder!\%%~nxa",%%b
)
echo ... done.

Open in new window

0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39941285
It sounds like you want the actual name of the lowest subfolder, which could be different on different lines.  I took a slightly different approach, hope this is helpful.   Seems to do what you want.  I also added some logic to correctly handle the trailing spaces before the comma, and quotes if they are found.  Here's the script and below that my test input and test output.

SCRIPT
@echo off
setlocal EnableDelayedExpansion

REM Define inout and output files, and folder strings to use
set SourceFile=input.txt
set TargetFile=input_new.txt
set OldFolder=X:\subX
set NewFolder=Z:\Data

echo Processing '%SourceFile%' ...

REM Capture all output and pipe to a new output file
(
  REM Read each line of the input test file, get the first comma delim parm, and the rest
  for /f "usebackq tokens=1* delims=," %%A in ("%SourceFile%") do (
    REM Remove any leading or trailing spaces from the first token
    call :Trim %%A
    REM Remove double quotes (if there are any)
    set Trim=!Trim:"=!
    REM Now process the first token as a file name and get it's parts
    for %%C in ("!Trim!") do (
      REM See if we have any subfolders or not
      if "%%~dpC" EQU "%OldFolder%\" (
        REM No subfolder, just write out the file and new folder
        echo "%NewFolder%\%%~nxC",%%B
      ) else (
        REM Subfolder found, extract the name of the lowest folder
        for %%D in ("%%~dpC.") do (
          REM Write out the file, lowest subfolder, and new folder
          echo "%NewFolder%\%%~nD\%%~nxC",%%B
        )
      )
    )
  )
) > "%TargetFile%"

echo ... done. 

REM Small subroutine to remove any leading and trainling spaces off a parm
:Trim [string]
  set Trim=%*
  exit /b

Open in new window

TEST INPUT
"X:\subX\file1.pdf"               ,"newfile1.pdf",<some string>,<some string>
"X:\subX\keepsub2\file2.pdf","newfile2.pdf", <some string>, <some string>
"X:\subX\anysub\keepsub3\file3.pdf","newfile3.pdf", <some string>, <some string>
X:\subX\anysub\keepsub4\file4.pdf  ,"newfile4.pdf", <some string>, <some string>

Open in new window

TEST OUTPUT
"Z:\Data\file1.pdf","newfile1.pdf",<some string>,<some string>
"Z:\Data\keepsub2\file2.pdf","newfile2.pdf", <some string>, <some string>
"Z:\Data\keepsub3\file3.pdf","newfile3.pdf", <some string>, <some string>
"Z:\Data\keepsub4\file4.pdf","newfile4.pdf", <some string>, <some string>

Open in new window

~bp
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now