Batch File Help Please ( reference a text file )

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
daviddc114
Posts: 19
Joined: 23 Jul 2013 12:45

Batch File Help Please ( reference a text file )

#1 Post by daviddc114 » 11 Jul 2014 04:42

I would like to write a specific batch program on a flash drive that i will use repeatedly, but on different computers so the directorys will change. Therefor i would like to put a .txt file that i can paste the directory in so that when the batch file runs, it looks into that .txt file and uses whatever is in there as the directories to use. how is this accomplished? thank you very much.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch File Help Please ( reference a text file )

#2 Post by foxidrive » 11 Jul 2014 05:56

The correct answer which works depends on your code.

People that read your question can make assumptions about how your code works
but it can be a frustrating experience when it's wrong, and then a to-and-fro starts
with 20 questions to find out exactly how the task is meant to work.

daviddc114
Posts: 19
Joined: 23 Jul 2013 12:45

Re: Batch File Help Please ( reference a text file )

#3 Post by daviddc114 » 15 Jul 2014 06:13

well it would be using different things. like xcopy or dir or rename or move ect.. i am just wondering if there is a way that whenever i enter a certain directory, instead of writing it in the batch file i can type a certain code that will reference a specified txt file, this way i can just paste in the directories i want to use in the specified txt files so i can just run the batch file, instead of having to adjust each batch file whenever the directory changes.. just wondering if there is a general command function for that.. thanks..

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch File Help Please ( reference a text file )

#4 Post by foxidrive » 15 Jul 2014 07:59

This will get the first line of a text file into a variable:

Code: Select all

@echo off
set /p directory=<"file.txt"
echo "%directory%"
pause

daviddc114
Posts: 19
Joined: 23 Jul 2013 12:45

Re: Batch File Help Please ( reference a text file )

#5 Post by daviddc114 » 17 Jul 2014 10:20

Im not sure if that will work exactly, what i have is this

Xcopy C:\Example\123\0.jpg C:\Destination\123\0.jpg

but i have hundreds of lines at a time to do this, and the starting point and folder names are constantly changing. Not the jpg, just the folder that it is in. So i would like to have 2 .txt files, one i can paste a location in and the other a file name

example-
location.txt has "C:\System\here" in it
Files.txt has "123" in it, and when i run the batch it was do this

xcopy C:\System\here\123\0.jpg

is this possible?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch File Help Please ( reference a text file )

#6 Post by Squashman » 17 Jul 2014 14:38

Code: Select all

@echo off
set /p location=<"location.txt"
set /p files=<"files.txt"
Xcopy "%location%\%files%\0.jpg" "C:\Destination\%files%\0.jpg"
pause

daviddc114
Posts: 19
Joined: 23 Jul 2013 12:45

Re: Batch File Help Please ( reference a text file )

#7 Post by daviddc114 » 18 Jul 2014 05:02

IT ALMOST WORKED PERFECTLY!!!

Maybe it was my example that wasnt clear enough, the location worked PERFECTLY, however the files.txt will contain a list of hundreds of file names

example

12422
1232456
12323
1231

it worked for the first line of 12422, but it did not work on the rest, i basically want the entire list of folder names in the TXT moved, they will all be in the same location as the location.txt, just a bunch of files.

daviddc114
Posts: 19
Joined: 23 Jul 2013 12:45

Re: Batch File Help Please ( reference a text file )

#8 Post by daviddc114 » 18 Jul 2014 07:56

I got it to work!!! thank you so much!

the problem i am having now is it works in a separate batch file, but when i add the text to my main batch program it will not work??? Is there some way to isolate this script from the rest so it will work within my main batch program? i have tried putting ( ) around the entire script but that did not work...

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch File Help Please ( reference a text file )

#9 Post by Squashman » 18 Jul 2014 08:22

daviddc114 wrote:I got it to work!!! thank you so much!

the problem i am having now is it works in a separate batch file, but when i add the text to my main batch program it will not work??? Is there some way to isolate this script from the rest so it will work within my main batch program? i have tried putting ( ) around the entire script but that did not work...

I am not omniscient. I have no idea what your final code looks like or how you ran it.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch File Help Please ( reference a text file )

#10 Post by foxidrive » 18 Jul 2014 09:16

daviddc114 wrote:So i would like to have 2 .txt files, one i can paste a location in and the other a file name


You got what you asked for, David.

The man of the house, a programmer:

My wife said: "Please go to the store and buy a carton of milk and if they have eggs, get six."
I came back with 6 cartons of milk
She said, "why in the hell did you buy six cartons of milk??"

"They had eggs"

Post Reply