How to copy from a constantly changing path in a batch file? - batch-file

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.

Related

Batch: How to run tree command from current directory?

I may be confusing current directory with working directory but regardless I am trying to make a batch file that runs the tree command from the folder it's currently in.
I have a folder called "Network_Switch_Backup" with a script, some other items and a subfolder called "backups".
This has worked for testing purposes:
tree U:\Desktop\Network_Switch_Backup\backups /f
But as I will be zipping it and sending it to different computers and users clearly this isn't practical since they could put the folder anywhere besides the desktop.
I've looked at other threads and amongst other things I tried, this looked the most promising (but still did not work):
tree %cd%\backups /f
However tree %cd%\downloads /f works perfectly fine when running from cmd so I'm just a bit confused.
It is advisable in batch files to reference executables to run with full qualified file name which means full path + file name + file extension, especially if the storage location of the executable is well known. That makes the batch file independent on the values of the environment variables PATHEXT and PATH. PATH is quite too often not correct defined on many computers running Windows.
The full qualified name of TREE is %SystemRoot%\System32\tree.com.
Environment variable SystemRoot is not defined as system or user environment variable like PATH and PATHEXT, but is nevertheless defined on execution of a batch file. So it is very safe to use this Windows environment variable.
What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? explains very detailed how Windows command processor finds executables and scripts not specified with full qualified file name on command prompt or in a batch file.
There are two directories which need to be taken into account on coding an application or script:
The application/script directory is the directory containing the program or script.
The current directory or working directory is the directory from which the program or script is executed.
For example a batch file is stored in directory "%UserProfile%\Desktop". Windows sets the directory of the batch file as current directory on simply double clicking the batch file on user's desktop. Therefore the script directory is the current directory on execution of the batch file. But if this batch file is executed by right clicking on the batch file and left clicking on context menu option Run as administrator, the batch file stored in "%UserProfile%\Desktop" is usually executed from directory %SystemRoot%\System32 depending on user account permissions and on user account control setting of current user. The reason for making %SystemRoot%\System32 the current directory before executing the batch file is explained in detail by answer on Why does 'Run as administrator' changes (sometimes) batch file's current directory?
The MSDN article Naming Files, Paths, and Namespaces explains in detail how to reference files and folders relative to current directory. The current directory is simply not included in file/folder argument string or alternatively represented by .\.
In case of drive and path of current directory needs to be known, for example to output it on running a batch file, there is the dynamic environment variable CD (short for Current Directory). %CD% or !CD! with delayed expansion enabled expands to full path of current directory which does not end with a backslash, except the current directory is the root directory of a drive. The help output on running in a command prompt window set /? explains dynamic environment variable CD briefly on last help page.
Batch files need to be designed very often to reference files or folders with a path relative to directory of the batch file. In this case it is not advisable to use the current directory because the current directory can be really any directory.
The help output on running call /? in a command prompt window explains how arguments of a batch file can be referenced from within a batch file. Argument 0 is always the batch file itself.
%~dp0 references drive and path of the batch file. This file path ends always with a backslash, but of course can contain a space or one of these characters &()[]{}^=;!'+,`~ which require entire file/folder argument string to be enclosed in double quotes. So %~dp0 must be concatenated in a batch file without an additional backslash after that string and the entire argument string must be enclosed in double quotes to work safely.
So the command line to use to reference the subdirectory Backups in directory of the batch file independent on which directory is the current directory is:
%SystemRoot%\System32\tree.com "%~dp0Backups" /F
A bug of cmd.exe explained in detail on What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory? should be taken into account on using %~dp0 in a batch file.

User CMD Batch Interaction

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.

Why does changing working directory to desktop subfolder using environment variable UserProfile not work?

I am making this code in batch, in which the directory needs to be changed in order for the commands to complete successfully. Right now, this is what I've discerned to use, from other posts asking the same question:
cd C:\Users\%UserProfile%\Desktop\NewFolder
From what I read, this should work, but it isn't. And I don't know why.
Can anyone please help me on this?
Read HELP SET and then try SET user and you will realize that USERPROFILE contains the full path of the current user home directory.
So, to set the current working directory to the user desktop, you need to either
CD /D %USERPROFILE%\Desktop
or
PUSHD %USERPROFILE%\Desktop
which is better, because you may later POPD to restore the current directory to its initial value.
Try cd %USERPROFILE%\Desktop\NewFolder
Use cd /D "%USERPROFILE%\Desktop\NewFolder"
Environment variable USERPROFILE contains full path to the profile directory of the current user. The user name stored in environment variable USERNAME which is included also in USERPROFILE can contain also 1 or more spaces and therefore it is better to use double quotes around complete path.
And the user's profile directory can be on a different drive as the current drive. So it is better to use also parameter /D on changing the current directory.
By the way: Executing in a new command prompt window the command set shows all standard environment variables with their current values.

Could someone help to understand this batch script?

I'm reading a batch file, but I do not understand it, can someone help to explain?
As I understand %0 is is the name of batch file, can we iterate on it? or is it a convenient way to represent a folder?
I can not find the variable %BatchPath% in the file, where do you think it's defined?
And it seems APATH is defined in the two loops?
for %%x in (%0) do set APATH=%%~dpsx
for %%x in (%BatchPath%) do set APATH=%%~dpsx
pushd %APATH%
You can iterate over a single value. It just means the set statement is executed once. The ~dps then strips the file name, so that only the directory remains.
The first line performs this action on %0, indeed the path and name of the current script.
The second line performs the same action on a given variable, Now that is the fun part, because if %BatchPath% is empty, nothing gets iterated, so the set statement on that line is not executed at all.
So effectively, it stores a directory, which is the directory of the script by default, but can be overridden by explicitly assigning a path to %BatchPath% before calling this script.
pushd allows you to save a directory, so you can return to it later using popd. It allows the script to jump to another directory an be able to restore the shell to the original directory before it terminates.
%0 is the current batch file.
%%~dpsx gives the current batch file's
short path here its giving the Drive name for eg "D:\"
Pushd Stores the name of the current directory for use by the popd command before changing the current directory to the specified
directory.
APATH is some variable used to store the path.
so basically the script is fetching details about the script file name , its drive location and storing it to be used as location from which last batch file ran or something like that.

Make a batch that detects its own filepath

I need to make a batch file that detects what drive and directory it is in. When I run the the file normally, it is in the correct directory/drive already. But when it is run as admin, it starts in system32. Is there a command that goes to the directory or drive the batch came from?
You could use
Pushd "%~dp0"
This changes the current directory to the path of the batch file.
Quoting the argument makes it safe against special characters in the pathname like "C:\Documents & Settings"
well a workaround is to use \ before the path to give absolute path.
So, if you need to run a file c:\temp\xyz.exe and even if you are in directory c:\winodws\system32 still when you do cd \temp\xyz.exe still the file will run properly

Resources