Search code examples
batch-filecmdxcopy

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.


Solution

    1. add the large file to the myexcludes.txt file so it wont copy

    2. 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)