Solved

Stripping a filename upto a certain subfolder in batch

Posted on 2014-03-19
3
458 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 85

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 55

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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

724 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