Solved

Stripping a filename upto a certain subfolder in batch

Posted on 2014-03-19
3
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 84

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 54

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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