Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

setting variables for additional processing in batch script

Posted on 2014-02-22
9
Medium Priority
?
240 Views
Last Modified: 2014-02-23
Hi EEE

I have a txt file with the following format:
ServerName\Dir1\file1.pdf newfile1_test.txt newfile1 n
ServerName\Dir2\file2.pdf bestfile2_test.txt bestfile2 b

etc

I want to write a batch script that does the following:

1. Read each line from the text file for a round of processing
   1.1 Store each of the four strings of the line into 4 vars  - source , destfile, destfolder, destpparentfolder
    1.2  Check if destparentfolder exists on the C:/ of server2.If it doesn't, create it.
        1.3 Check if destfolder exists under C:/destparentfolder. If it doesn't, create it.
          1.4 Put the destfile in the C:/destparentfolder/destfolder.
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
9 Comments
 

Author Comment

by:LuckyLucks
ID: 39879948
In our example above, ServerName\Dir1\file1.pdf will be stored under C:/n/newfile1 as newfile1_test.txt
0
 
LVL 28

Accepted Solution

by:
Bill Bach earned 1900 total points
ID: 39880581
It is not clear it you want to write a batch script and you have a specific question, or if you want someone to write the entire batch script for you.  In any event, this script should do it:

FOR /F "tokens=* delims=" %%x in (input.txt) DO call :doit %%x
exit

:doit
if not exist C:\%4 MD C:\%4
if not exist C:\%4\%3 MD C:\%4\%3
copy %1 C:\%4\%3\%2


It will read the INPUT.TXT file and call DOIT for each line of the source file, which then do each of the three steps you asked for.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 39880672
1. I think you might get a simpler solution using xcopy or robocopy, since they are capable of creating the destination paths they need.

2. I do not understand the control statements from your posted example and description.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:LuckyLucks
ID: 39880897
Bill, How do I get rid of the overwrite and the is the file a file or directory (when no overwrite)?
0
 
LVL 28

Expert Comment

by:Bill Bach
ID: 39880919
I figured that the batch file part was the hard part, and that you'd know how to use the COPY command.  The help screen for the COPY command comes in handy here:

 /Y           Suppresses prompting to confirm you want to overwrite an existing destination file.

The switch /Y may be preset in the COPYCMD environment variable. This may be overridden with /-Y on the command line.  Default is to prompt on overwrites unless COPY command is being executed from within a batch script.


So, add the "/Y" switch after "COPY" to avoid the prompt.  You can also use XCOPY, RoboCopy, or a host of other command-line copy tools.  See each tool's specific command line options for properly using it.
0
 
LVL 58

Assisted Solution

by:Bill Prew
Bill Prew earned 100 total points
ID: 39880987
Ne real need for a subroutine here, so just to show a slightly more compact approach:

@echo off

set ControlFile=c:\temp\control.txt

for /f "usebackq tokens=1-4" %%A in ("%ControlFile%") do (
  if not exist c:\%%D\ md c:\%%D
  if not exist c:\%%D\%%C\ md c:\%%D\%%C
  copy /Y %%A c:\%%D\%%C\%%B
)

Open in new window

~bp
0
 

Author Closing Comment

by:LuckyLucks
ID: 39881347
Thank you all.
0
 

Author Comment

by:LuckyLucks
ID: 39881360
With a /Y I still get - Does C:\a\abc\abcfile1.pdf specify a file name or directory name on the target <F=file, D=directory>?
0
 
LVL 28

Expert Comment

by:Bill Bach
ID: 39881434
Switch to XCOPY, and thay should go away.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

610 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