Solved

Stripping a filename upto a certain subfolder in batch

Posted on 2014-03-19
3
436 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 53

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

17 Experts available now in Live!

Get 1:1 Help Now