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)
Related
I wanna write a bat file to set system variables on any windows system. My software which I made needs to set a path instead of asking the user to process the method I heard we can do this task using bat files so I tried multiple ways to set a path nut most of them are duplicates of all existing paths instead of adding new ones and some times it's removing all paths and keeping only the new path
setx Path "%Path%;c:\ffmpeg"
which is duplicating all the existing path variable
I did read multiple Stackoverflow queries on this but none helped
if ur testing on your system to check
please save/make a copy of all ur paths first
Thank you
Eswar
i wouldn't recommend you to mess with system variables but if you want to store path for your program here's a script that will do the thing but before doing so i would encourage you to make make a backup for files and all the paths stored in system variable %Path% and if something happen read this but anyway here is a script that worked for me:
#echo off
echo validating Path
ping localhost -n 2 >nul
set "store_path=Path to your program "
rem /M to set the variable in system wide environment
setx Path "%path%;%store_path%"
setx Path "%path%;%store_path%" /M
simply i am storing the new path with the old one.
I don't suggest you mess with system variables but if you want to store path from cmd use
setx Path "%Path%;C:\your\path\here\" C:\ffmpeg\bin
I tested on windows 10 it worked for me
Hopefully it works for u too!!
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 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.
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.
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.