Solved

Stripping a filename upto a certain subfolder in batch

Posted on 2014-03-19
3
452 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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