I am creating a batch file that will install files into a certain location on a computer. this could be used for various things such as game mods, folder backups, etc. If i am trying to have the user type in the path they want the file to be sent to. How would I do that? I would like to have that path pasted into a .txt file, that way i can access it later in the program. I have tried the ">" and it works on putting the path into the file. How do I extract that information. "Direct Question" How can I take user input, and use it to paste files somewhere else in the computer using the user's path that was specified.
How many questions do you want for your money?
To input a string:
set /p "installdir=Install in which directory? "
At any point thereafter, you can use %installdir% to access the directoryname input.
If you need to save the directoryname in a file, then use
>"c:\wherever\you\want\filename.txt" echo %directoryname%
and to read it,
for /f "usebackqdelims=" %%a IN ("c:\wherever\you\want\filename.txt") do set "dirread=%%a"
or
set /p "dirread="<"c:\wherever\you\want\filename.txt"
and "pasting" or probably copying,
copy /b "x:\directory from\filename.whatever" "%dirread%\"
to copy the file "filename.whatever" from directory "x:\directory from\" to directory in the environment variable dirread as read by one of the previous two methods or substitute directoryname for dirread to use the directory name originally entered.
Related
I'm trying to copy a folder with a variable path to a fixed path using a .bat file. Basically trying to retrieve a folder from a shared server to use it locally. The path to the variable location can mostly be solved by retrieving an user input. However, the user input will only cover a large part of the to retrieve path. At the end there is still a variable component. That's where I'm stuck. Is there any way to use a wildcard in .bat to retrieve a folder? Searching stackoverflow teaches me that it can only be used for files and not folders, is there a workaround?
Code:
#echo off
set /p enddirectory=enter your folder:
set fixedpath=C:\xxx\xxx\folders\
set endpath=%fixedpath%%enddirectory%\ (*wildcard to be added here)
Xcopy /e %endpath% "C:\xxx\localfolder"
For example:
Server path : C/xx/ABC-1234567
User input: ABC
Defining the first path of the server path works, how can I get the full path? (1234567 is variable)
How would I get a batch file to copy from a certain directory that contains a file with a name that would be different every time the batch file is run.
Ex.:
copy "C:\Users\%USER%\AppData\Local\Microsoft\Windows\INetCookies"
%USER% is the changing file name.
%username%
is the environment variable for the current user's username. and
%userprofile%
is the path to their profile.
In addition to the already provided answer, you can get even further down the path using another of the built-in environment variables, %LOCALAPPDATA%.
"%LocalAppData%\Microsoft\Windows\INetCookies"
Type SET followed by [Enter] into a cmd.exe window to see all of the machine's variables listed.
I'm trying to create a batch file that can look through contents of network shares to find a specific named directory and subdirectory and then map a drive to the share that contains these. Problem being that, whilst the contents of the share have these folders in them to identify that this is the correct share, the share name itself may be different in different setups, so I can't just use the share name in the script.
In other words
\\server\share\sims\setups
the sims and setups directories will ALWAYS exist in the share, but the share name is the thing that can be different. The server name will already exist in the batch file as it is set by using the set /p command to prompt the user for the servers ip.
i have looked at the find and the forfiles commands but these seem to only work to search locally and not on network paths.
Thanks
As indicated by foxidrive you can use the net view command to list shares of a server.
The for command is able to iterate over output from commands. Combining these in the following rough commandline does map a drive for me if a certain folder/file is present.
for /F %a in ('net view \\srv') do if exist "\\srv\%a\sims\setup" net use z: \\srv\%a
(don't forget to change %a to %%a if you use this line in a command-script)
The net view does output some noise but that doesn't screw the result. You could add some extra handling to only handle shares after the '-----' line is received.
How does this work?
net view \\server outputs the following:
Shared resources at \\server
Awesome server
Share name Type Used as Comment
-----------------------------------------------------------------------------
Configuration Disk System Configuration
Download Disk Download Share (Full Access)
Public Disk Public Share (Full Access)
Blah Disk Blah share (Full Access)
The command completed successfully.
The for /f command parses each line up to the next space. From the FOR /?
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]
...you can use the FOR /F command to parse the output of a
command. You do this by making the file-set between the
parenthesis a back quoted string. It will be treated as a command
line, which is passed to a child CMD.EXE and the output is captured
into memory and parsed as if it was a file.
Using that technique would echo the following result if run on net view \\server:
Shared
Network
Share
-----------------------------------------------------------------------------
Configuration
Download
Public
Blah
The
The EXIST operator checks if a file/directory exists. By concatenating the servername, share and the expected folder you can find out if that folder does excist on that share.
As I said the first 4 lines and the last line are noise and should be ignored but they don't hurt too much in this scenario.
I want to copy the files from current folder to another location like (CURRENT FOLDER) file name like this (F0#CGDBANG000947532#) to another location like (\10.10.10.1\BasketsIn) with user name in the file name like (F0#CGDBANG000947532#logesh) at the end F0#CGDBANG000947532# copy to F0#CGDBANG000947532#username
thanks
Easy:
copy "c:\A" "d:\%username%_A"
EDIT
finally, after some of your comments here and in your other question, I understood your request (I think).
#echo off
for %%i in (%*) do if /i "%%~xi"==".eps" copy "%%i" "\\10.10.14.13\adman\in\displ\%%~ni%username%.%%~xi"
pause
If your file names do not have an extension or if you want the user name to be appended after the very end of the name (i.e. even after the extension), you could just use the following simple command:
COPY * \10.10.10.1\BasketsIn\*%USERNAME%
where USERNAME is a system environment variable that resolves to the user name of the current user.
If, however, you have names with extensions and you want the user name to be appended after the file name but before the file extension, you could use the ? mask character like this:
COPY * \10.10.10.1\BasketsIn\???????????????????????????????%USERNAME%.*
Just make sure you have provided enough ?s to cover the longest possible name in your case. If you are interested, you can learn more about this method in this excellent, in-depth Super User answer by dbenham:
How does the Windows RENAME command interpret wildcards?
One more note: this method may not work as expected with file names that have "multiple" extensions, i.e. like some.txt.doc.
I need help making a batch file in ms dos to do certain commands like:
Request from you to first press any key
List the contents of the C:\WINDOWS directory
Create a subdirectory on one of your local drives. Use your initials to name the subdirectory.
Copy all the text files from C:\WINDOWS directory into the new subdirectory.
Print one of the text files copied to your new subdirectory.
for 5. I think I need to make a call to a command to print
You need to create a .bat file that has the following text:
a)Request from you to first press any key
pause
b) List the contents of the C:\WINDOWS directory
dir c:\windows
c) Create a subdirectory on one of your local drives. Use your initials to name the subdirectory.
md c:\ro
d) Copy all the text files from C:\WINDOWS directory into the new subdirectory.
copy c:\windows\*.txt c:\ro
Regarding printing files I'm not sure, if I remember correctly the simplest way is something like:
type file.txt > LPT1
To create a .bat file, you should type in:
copy con filename.bat
... print the relevant lines above
When finished press Ctrl-Z and Enter.
So, all in all, you should type in:
copy con filename.bat
pause
dir c:\windows
md c:\ro
copy c:\windows\*.txt c:\ro
type file.txt > LPT1
And then Ctrl-Z and Enter.
Update: If you don't want the commands to be printed to the user, you can add another line before pause that will contain #echo off.
Check out Rob van der Woude's Scripting Pages, specifically the Batch Files section. He has an excellent reference with lots of tips and tricks that will help with most of these tasks.
Command line help is your friend here for most of this. You will need to have input parameters specified for a number of the things you;re asking for, though.
As a quick search, try this article.