Replace file in "changing" directory - batch-file

I know the title doesn't make sense so I'll try to elaborate:
I have a program called My App.exe and when I run it, it creates a file in a folder in AppData. The name of the folder it creates varies for every machine. Example:
When My App.exe is run on one computer, it creates this file:
%LocalAppData%\Kazankoph\My_App.exe_Url_3p43oix65iqigwb4lndfi4m34sf1xjg3\1.2.0.0\Settings.dat
When its run on another machine it creates this file:
%LocalAppData%\Kazankoph\My_App.exe_Url_6f47ntd26lwubpr3hunvt2m67sf1xtq2\1.2.0.0\Settings.dat
The only things that stay the same are the parent folder "Kazankoph", the child folder "1.2.0.0" and the file name settings.dat
It seems that the text that comes after "My_App.exe_Url_" is always 32 characters long, and is randomly generated
So my Dilemma:
I need to create a command (using cmd, batch, or SciTE script) that will take the updated settings.dat file and overwrite the old one.
I used XCOPY with the Y switch to overwrite without prompt:
XCOPY settings.dat "%LocalAppData%\Kazankoph\My_App.exe_Url_6f47ntd26lwubpr3hunvt2m67sf1xtq2\1.2.0.0" /y
But the problem with that code is that it only works for that one computer.
I want a code that will work on any computer regardless of the 32 character code.
something like this:
XCOPY settings.dat "%LocalAppData%\Kazankoph\My_App.exe_Url_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\1.2.0.0" /y
Of course this one doesn't work but you get the idea.
Anyone have any suggestions?

Run this and it should give you the folder, and copy the file from the current directory into the folder.
#echo off
for /f "delims=" %%a in (' dir /b /s /a-d "%LocalAppData%\Kazankoph\Settings.dat" ') do set "folder=%%~dpa"
echo copying settings.dat to "%folder%"
copy /y "settings.dat" "%folder%\"
pause

Related

Setting up Source and Destdir in a batch

for /L %%f in (1,1,10) do copy File1.txt %%f.txt
this code does the job very well, but I'm trying to understand how to change it to make it read
subfolders, so that I don't have to keep moving the batch file to every folder
I saw this, but not really sure how to put it together
#echo off
SET "sourcedir=C:\Users\user\Desktop\Main\Original"
SET "destdir=C:\Users\user\Desktop\Main\Copied"
for /L %%f in ('dir /s 1,1,10) do copy *.txt %%f.txt
in the section - copy *.txt %%f - I put a * so that it can only look for .txt files, but this action
slows down the coping and then stops working.
I know my code is a mess, just trying to put something together
I have many Subfolders and each folder has 1 txt file in it with all random names
and I need to make multiple copies of each file.txt in each folder
I have so many subfolders that it would literally take me months of time to get all files copied
and by then I would have many more new files to work on
so getting this copier to read Subfolders is like top priority for me.
I would like help putting this together and then explaining how it links
because I'm interested in applying the Set and dir to other batch file I have
Please any details on this will be much appreciated
I was told to look into xcopy and robocopy, but I have no idea were to add a counter
#echo off
for /1 %f in (1,1,10) do xcopy "C:\Sources" "C:\Target" /c /d /i /y
exit
So I have this that reads from source and dumps in main folder where the batch is
Option 1)
for /L %%f in (1,1,10) do xcopy "C:\Source Address\*.txt" %%f.txt
Option 2)
for /L %%f in (1,1,10) do xcopy "C:\Source Address\" "C:\Destination Address\ %%f.txt"
The thing I don't like is that is asks me a question
and I have over 10,000 txt files, I can't sit here and press F for Filename
10,000 times, can we disable that
OK, so I got this working
all I need help with is were to add this /c /d /i /y
I am still trying to get it to read Subfolders with the batch sitting
in the main folder and me not having to move files back and forth
Option 3)
for /L %%f in (1,1,110) do copy "C:\Source Address\*.txt" %%f.txt`
This works well with the Source and the wild card #magoo told me to add
the Source before the .txt file
But with this code I would still have to open hundreds of folders and move
the file to the source run the copier and then move back all copied files
Still need help with Subfolders
for /L %%f in (1,1,10) do copy File1.txt %%f.txt
will vary %%f from 1 (the first number in the parenthesised list) to 10 (the last) in steps of 1 (the middle) and will therefore copy file1.txt to 10 separate files, 1.txt to 10.txt.
Since there are no paths specified, the copy will be performed from file1.txt in the current directory to 1.txt .. 10.txt in the current directory.
If you were to put your batch file in any directory that is mentioned in the path variable (just execute path at the prompt to show it) then no matter where the current directory is, you could just use that batch filename to execute the batch, and the files would be created by copying file1.txt in the now-current directory to 1.txt .. 10.txt in the now-current directory - if file1.txt exists in the now-current directory, and if not, it will generate error messages reporting that the source file is missing.
If you were to replace file1.txt in the batch with "x:\wherever\file1.txt" then the file would be copied specifically from the file "x:\wherever\file1.txt" regardless of whether file1.txt exists in the now-current directory. (And please get used to "quoting file or pathnames" as it will avoid a whole slough of problems when you tackle names containing spaces and some other special characters).
I have no idea how for /L %%f in ('dir /s 1,1,10) do is supposed to work since according to Hoyle, the first element in the parenthesised list should be a number. I'd suggest that we have a small transcription problem here.
Th slower and slower problem - yes, understandable. You are creating more and more .txt files, and copying all of them to a new destination...
Perhaps referring to This response and question might show how to solve your subdirectory-scan issue.

Copy files matching the names in a list of paths and paste them in one new folder?

I'm working in a windows 7 machine and I'm trying to take all of the files matching the names in a list of file paths (I have the list saved as a csv, rda, and can make a txt file if needed). Ie: the list looks like:
Y:/iglgrelkgjkrle/originals/jsfhdjk.xls
Y:/iglgrelkgjkrddsle/ffhej/originals/jsfhdjk.xlsx
Y:/kssrldsse/ffhej/originals/jsfhdjk.xlt
Y:/blahblah/blah/blahhh/blahhhhhh/originals/blahahaha.pdf
...
...
And basically I want all of these files in this list copied to a new folder in a different location. Thanks!
Almost any problem can be solved with a FOR statement in windows command processor. Using for /f we can search a list in a text document and for each item (This case; location) specified, can run a command to copy it to a new location.
For copying the file, xcopy will be very handy as it has many copy option switches we can use such as /i /z /y.
/I - If in doubt always assume the destination is a folder
/Z - Copy files in restartable mode. If the copy is interrupted part way through,
it will restart if possible.
/Y - Suppress prompt to confirm overwriting a file. (Use /-Y for reverse)
In the following commands bellow, C:\list.txt is used as an example. This is where you specify the location of your list file. This can support a wide range of file formats including html. It does not hurt to try your extensions.
For the place to output the copied files - C:\CopyFolder is an example of the location of the folder you wish to send them too. You can also send them to a local server via \\server\folder\.
From command Line:
for /f "delims=" %i in (C:\list.txt) do (xcopy "%i" "C:\CopyFolder" /i /z /y)
From batch file:
for /f "delims=" %%i in (C:\list.txt) do (xcopy "%%i" "C:\CopyFolder" /i /z /y)
If this has solved your issue, please don't forget to mark this response as solved. I will be happy to further explain any questions!

How can you use Windows CMD to save a copy of a file name to a blank text document?

I've been experimenting with Xcopy on my flash drive, which as the reader most likely knows, is a way to transfer multiple file copies very efficiently. During my experimentation, i had a thought. The code i ran(which will be disclosed at the end of the comment) was set to copy files from my desktop, music, photos etc. However, on my desktop was a particularly large file for a game i frequent. When the program got to the games files, it took an extremely long time to copy the files, as the game ran a 3D environment, and i needed this code to run quickly and efficiently. So, my thought was:Is there a way to copy the name of a file(in this case, the name of the game, FTB), have the program copy the name of the file only, then create a text file on the flash drive where the code is being run, then overwrite the name of that document with the copied name?
Here is the code being run, and i am aware that it has some obvious problems, but its being worked on:
#echo off
:: variables/min
SET odrive=%odrive:~0,2%
set backupcmd=xcopy /s /c /d /e /h /i /r /y /EXCLUDE:MyExcludes.txt
%backupcmd% "%USERPROFILE%\Pictures" "%drive%\all\My pics"
%backupcmd% "%USERPROFILE%\Favorites" "%drive%\all\Favorites"
%backupcmd% "%USERPROFILE%\Videos" "%drive%\all\Vids"
%backupcmd% "%USERPROFILE%\Documents" "%drive%\all\Docs"
%backupcmd% "%USERPROFILE%\OneDrive" "%drive%\all\Onedrive"
%backupcmd% "%USERPROFILE%\Desktop" "%drive%\all\Desktop"
%backupcmd% "%USERPROFILE%\Network" "%drive%\all\Other devices"
copy "Bookmarks.bak" "all"
removedrive D: -L -H -A -W:1000
if you need any extra information, just ask.
add the large file to the myexcludes.txt file so it wont copy
just create a new file on your backup drive using something like the following (assuming your file is called mylargefilesname.ext)
echo mylargefilesname.ext > "%drive%\location\mylargefilesname.ext"
------ edit ------
To do this for all .exe files you can exclude *.exe and then use a loop.
for /f %%f in ('dir /b "%USERPROFILE%\Pictures\*.exe"') do echo %%f > "%drive%\all\My pics\%%~nxf"
this is looping over the results of dir /b "%USERPROFILE%\Pictures\*.exe"
for each line of the output it will echo the full file path to your new folder using the filename + extension of the file (thats what the nx part does)
I know this is resurrecting an old thread, but I've found this code all over the internet (it's been copied a lot) with no clear explanation of the nonsensical %odrive% initialization and the non-initialization of %drive%.
It doesn't work because they left out some code. Here's how to fix it:
Replace:
SET odrive=%odrive:~0,2%
With:
SET odrive=%CD%
SET drive=%odrive:~0,2%
The first line gets the current directory as %odrive% and the second line strips out the first two characters (e.g., C:) and assigns them to %drive%
Note the script assumes it's being run from the removable drive. So you could simply replace all instances of %drive% with . and it would work just as well.

Batch file to rename images for WD Live TV

Apologies if this has been answered, the search gives something similar but not exactly what I'm after.
I use XBMC and a Western Digital TV Live as media centers. XBMC uses folder.jpg for movie box art. However, the WD uses both folder.jpg and moviename.jpg.
My folder structure is as follows:
Films\movie1\movie1.mkv(or avi etc...)
Films\movie1\folder.jpg
Films\movie2\movie2.mkv (etc...)
Films\movie2\folder.jpg
What I'm after is a .bat file that will scan the entire films directory, copy the folder.jpg and rename the new jpg using the name of the folder that folder.jpg is in. The original folder.jpg should remain.
Essentially from:
Films\movie1\folder.jpg
To:
Films\movie1\folder.jpg
and
Films\movie1\movie1.jpg
This should happen to each folder in Films.
I'm sure this must be possible but to be honest my knowledge of .bat files is very limited.
You can use a FOR /D loop to iterate over subdirectories of Films:
#ECHO OFF
FOR /D %%I IN ("D:\Path\to\Films\*") DO (
COPY "%%I\folder.jpg" "%%I\%%~nxI.jpg"
)
In the loop, the subdirectory's full path is referenced as %%I and its name alone as %%~nxI (could be just %%~nI if the name never includes a .).
You could run the loop directly from the command prompt, but you'd need to replace the double % characters with single %:
FOR /D %I IN ("D:\Path\to\Films\*") DO COPY "%I\folder.jpg" "%I\%~nxI.jpg"
Please note also that if a moviename.jpg already exists, the COPY command will stop for confirmation of overwriting the file. If you just want to overwrite it anyway without manual confirmation, add the /Y switch:
COPY /Y ...
for /d %%a in (*) do if exist "%%~a\folder.jpg" echo copy /b "%%~a\folder.jpg" "%%~a\%%~na.jpg"
Remove the echo to get it working.

xcopy does not create directory structure

I have a strange problem with xcopy in Windows XP Professional. I don't know if its a stupid question as I am specifying only a file as the source, so should I even expect any other behavior ? This is it:
I am using xcopy <src> <dest> /s/y.
<src>=C:\sourcefolder\a\b\c\d\something.java and
<dest>=C:\destinationfolder.
Now xcopy copies the file but does not create the directory structure \a\b\c\d\ inside C:\destinationfolder .
what I want is C:\destinationfolder\a\b\c\d\something.java and
what I get is C:\destinationfolder\something.java
I have tried to run it in destination folder C:\destinationfolder by specifying a . for target folder
Tried it without any target in above
There is a script I have which calls xcopy iteratively so I am left with C:\destinationfolder\many java files without any directory structure.
A. Yes I have done xcopy /? to see all options
B. /T also does not create any empty directory structure
C. I can not go to source folder a\b\c\d\ and run xcopy . <dest>
UPDATE
I removed my previous answer on using ROBOCOPY. I believe the following will do what you want using XCOPY.
Assuming your folder structure is like this:
SOURCE = C:\MyJavaStuff\A\B\C\D\something.java
DEST = C:\MyDestination
Run XCOPY like this:
XCOPY C:\MyJavaStuff\something*.java C:\MyDestination /S /E
Note the * in something*.java.
The problem is that you are specifying which file to copy in the source. xcopy won't create the folder structure in this case. However, if you change your call to xcopy to
xcopy *.java C:\myfolder /s/y
it will copy the .java files and the folder structure as well. You need to specify a wildcard for this call to work as you want. If you want only to copy specific files, you will have to adjust the call to xopy, e.g.:
xcopy something.jav* C:\myfolder /s/y
Edit
You say that you get the list of files to copy from another command. If you can output this list of files in a text file, you could do the following:
FOR /F "tokens=* delims=," %F in (d:\test\list.txt) DO xcopy src\%~nxF* .\dest /S /Y
What this command does is read a text file ("d:\test\list.txt" in this case), read every line, and for each file, run xcopy, adding a wildcard at the end of the file name to make sure it creates the folder structure.
I'm assuming here that:
You can get the list of files in a text file, with only the file names (and optinally the paths)
You know the source folder ("C:\sourcefolder" in your example, the folder structure "a\b\c\d" does not need to be known) and can use it in the FOR command.
You can also use the following form:
FOR /F "tokens=* delims=," %F in ('cmd') DO xcopy src\%~nxF* .\dest /S /Y
where cmd needs to be replace with the command you use to generate your list of files to copy.
Note that if you use this FOR command in a batch file, you need to replace %F with %%F (and %~nxF* with %%~nxF*).
I had a look at the xcopy switches and you can copy the directory structure with /T, although that doesn't copy empty directories you can override this with /E. So your command would look like this:
xcopy C:\sourcefolder\a\b\c\d\something.java C:\destinationfolder /T /E /S /Y
Hope this helps!
In order to get C:\destinationfolder\a\b\c\d\something.java XCOPY needs to know how much of C:\sourcefolder\a\b\c\d\something.java to duplicate.
You can use:
C:
cd \sourcefolder
XCOPY something.java* C:\destinationfolder\ /S
Just be aware that this may have the side effect of also copying C:\sourcefolder\oops\something.java to C:\destinationfolder\oops\something.java as well as any other matches for something*.java under C:\sourcefolder\.
It seems to me that xcopy is typically used for copying directory trees, not single files (though it can work). And, xcopy will recreate the directory structure under the source folder in the target folder. If xcopy is given the /i switch, the target folder is assumed to be a directory. It will be created if it does not exist, even if there are multiple parents that need to be created.
You have C:\MyJavaStuff\A\B\C\D\something.java - that is your source. You want to end up with something.java not in C:\destinationfolder, but in C:\destinationfolder\A\B\C\D - so that is your target. You don't even have C:\destinationfolder. That is OK, with /i the entire path will be created.
xcopy /i c:\MyJavaStuff\A\B\C\D\something.java C:\destinationfolder\A\B\C\D
If something.java were the only file under C:\MyJavaStuff, you could also use
xcopy /sei c:\MyJavaStuff C:\destinationfolder
That would recreate the entire tree structure, copying your file. But if there are other files (and folders) under MyJavaStuff they would also be copied.
I have written a very similar batch file using xcopy. Perhaps what I did will help you.
This is the command I used:
xcopy "c:\Data Files\Dave's Data\*.*" "m:\Dave's Data" /R/D /E/H
In this case, Dave's Data on the source contains an entire directory tree containing at least 50,000 files & exceeding 75GB data. It runs perfectly on Windows XP
I found /T was unnecessary as the directory tree is copied. I also found /S was unnecessary as /E copied directories & sub-directories including empty ones. I included /R to copy & overwrite read only files on the destination. /H copied hidden directories. /D copied only newer files. I use this as a daily backup tool for my data.
The only problem I have is while this command will work on Windows 7 the first time, it will not work on subsequent runs when the destination directory tree exists. I suspect this is due to a privilege issue as the xcopy command will work on subsequent runs on Windows 7 within a cmd.exe window.

Resources