I am attempting to create a batch file to copy several files listed in a text file to a new folder. I have found several threads relating to this, but I can still not get the batch to work properly. The problem I am encountering is that the files listed in the txt are all in different source locations and have different extensions. The list reads, for example:
C:\Users\Foo\Pictures\Photographs\September\P1030944.jpg
C:\Users\Foo\Videos\Art\Movies\Class\movie.avi
C:\Users\Foo\Music\Jazz\20051.mp3
...etc
All the copy commands I could find have to list either the source directory i.e.
set src_folder=c:\whatever\
set dst_folder=c:\foo
for /f %%i in (File-list.txt) DO xcopy /S/E/U "%src_folder%\%%i" "%dst_folder%"
or the extension i.e.
for /R c:\source %f in (*.xml) do copy "%f" x:\destination\
but I need it to gather that information from the list itself.
If it helps I know that there are only files of a possible 39 different specific extensions in the txt (*.jpg *.gif *.png ... *.xhtml *.xht)
Any help/ideas?
Start reading HELP FOR and then try the following at the command prompt
FOR /F %a in (input.txt) DO #ECHO COPY %a c:\newfolder\%~nxa
you can see that %a gets expanded to the actual line in the input file, and that %~nxa is a way to extract the name and the extension from the file.
After careful testing, move the command to your BAT file, replace %a to%%a, and remove the ECHO command
#echo off
SET destfolder=c:\newfolder
FOR /F "delims=" %%a IN (input.txt) DO COPY "%%a" "%destfolder%\%%~nxa"
notice the wraping of the names with quotes "; and the inclusion of the "delims=" option; both are needed in case filenames contain blanks.
Finally be careful with possible name duplicates in the destination folder. If that is possible, you need to find an strategy to cope with such collisions. But this can be the subject of another SO question, can't it?
One sample which worked for me...
Replace my directories C:\whatever and C:\temp\svn with yours...
assuming that your filelist is named antidump_list.txt and located under C:\temp\svn\
> set src_folder=C:\whatever
> set dst_folder=C:\temp\svn
> for /f %%i in (C:\temp\svn\antidump_list.txt) DO copy "%src_folder%\%%i" "%dst_folder%\%%i"
Regards,
Gottfried
I have found that the easiest way to do this is to use a powershell script.
$Files = Get-Content File-list.txt
$Dest = "C:\output"
foreach ($File in $Files) {
Copy-Item $File $Dest
}
If you need to run it from a batch file, paste the above script to file named CopyFiles.ps1 and add the following command to your batch file
powershell -executionpolicy bypass -file .\CopyFiles.ps1
Since, powershell is included by default on Windows7 and newer, this method is as easy as doing the same with batch commands only.
Related
I am new to the coding here,
I was googling out several threads here regarding my requirement, tried but not getting succeeded.
My basic requirement is.
I have a folder having 1000 images.
Need to copy specific images and transfer to some other folder.
This is the basic basic requiremnet
Open to any way
Either Xcopy or robocopy or powershell
Tried with Xcopy
xcopy /d /y /s "\Your Image Folder\*.jpg" "C:\Users\%username%\Desktop\Master Image Folder\"
Here I am getting issues that One file is getting copied fine but not I need to copy several files . How will modify on that script
Tried with robocopy
robocopy "source path" "destination path" "file name"
same issue , how can I transfer multiple files.
Say I have listed down the names in excel or notepad.
Tried with PowerShell
Get-Content e:\np.txt | ForEach-Object {copy-item $_ E:\day1\sorted}
Here, The NP.txt - is the list of files in that notepad file.
Now the destination is E:\day1\sorted
But I get confused where will be the source path, Where should be the source path, should I write
to read your list, use a for /f loop:
for /f "delims=" %%a in (np.txt) do echo %%a
Instead of echo %%a (for testing), just insert your copy command:
for /f "delims=" %%a in (np.txt) do (
xcopy /d /y /s "\Your Image Folder\%%a" "%userprofile%\Desktop\Master Image Folder\"
)
(you may have to adapt it a little bit depending on the exact content of np.txt. Also, I'm not sure, why you use /s - shouldn't be necessary according to your description)
I have a set of folders in a drive and need to extract the path and filename of all of the files who have similar names (i.e., index_lms.htm or index*.htm). I need these to go into a csv file, which I will manipulate and use in a mass upload template. Is this possible through cmd shell or a batch file? (It's been a while since I tried writing these.)
Today the size of the task got worse....... I now have 1000 English courses, and translated versions in German, Turkish and Russian. So I really needed a batch solution to extract these pathnames - thank you.
One other issue arose in the the filename cannot have spaces and I see that all of them have multiple spaces in their filenames. Is this easy to edit as the paths/filenames are read?
Quick answer to Gung - the files are loaded into an LMS system and the target file for elearning is normally index.html or index_lms.html or index_html5.html. However the LMS also serves up documents (my current issue) in my case they are all pdf with a standard naming convention
This batch will iterate the given folders and recursively search them for any file beginning with index and create a csv with a header and the columns File, Path and PathFile.
#Echo off
SetLocal EnableExtensions
( Echo "File","Path","PathFile"
For %%A in (
"X:\Test\"
"Y:\Home\Customer\"
"Z:\Users\UserName\"
) Do Call :Search "%%~A"
) > "%~dpn0.csv"
Goto :Eof
:Search
For /R %1 %%B in (index*.htm*) Do Echo "%%~nxB","%%~dpB","%%~fB"
The output file will have the same drive, path and name as the batch file but the extension .csv
This will create file out.csv. Substitute your own dir pattern match.
del 2>nul out.csv
for /f "delims=" %%f in ('dir/s/b c:\index*.htm') do <nul set /p =%%f,>>out.csv
Here is another way to do it in PowerShell.
PS C:\src\t> type .\f2csv.ps1
$dirs = 'C:\src\t', 'C:\labs'
foreach ($dir in $dirs) {
Get-ChildItem -File '*.txt' -Recurse |
Select-Object #{Expression={$_.FullName}; Label="filename"} |
Export-Csv -Path '.\f2csv.csv' -Delimiter ',' -NoTypeInformation
}
PS C:\src\t> .\f2csv.ps1
PS C:\src\t> type .\f2csv.csv
"filename"
"C:\src\t\iosloginfo\lognextnum.txt"
"C:\src\t\t\file1.txt"
"C:\src\t\t\file10.txt"
"C:\src\t\t\file2.txt"
I have lots of files that need to go in specific folders. All the filenames have a certain pattern to them and all the folders that need to hold them.
How can I write a batch file with the following rule?
c:\s1_d1111_c1_p1.mpz needs to move to folder c:\SNC1111
c:\s1_d2222_c1_p1.mpz needs to move to folder c:\SNC2222
The filenames always have 4 digits that correspond to the folder. The number after s and c are always 1, and p can have any whole number after it.
This is for Windows Server 2008 R2 Standard
Thanks for your help!
Using Powershell
This sample move the files startin in jdk to a child folder jdk
$files = Get-ChildItem -File
for ($i=0; $i -lt $files.Count; $i++) {
$outfile = $files[$i].Name
if ($outfile.StartsWith("jdk")){
mkdir -Path "jdk" -Force
move $files[$i].FullName "jdk"
}
}
Modify to match your condition and paste the code in a Powershell window
#echo off
setlocal enableextensions
set "source=c:\"
set "targetBase=c:\SNC"
for /d %%f in ("%source%\"
) do for %%a in ("%%~ffs1_d????_c1_p*.mpz"
) do for /f "tokens=2 delims=_d" %%b in ("%%~na"
) do echo move /y "%%~fa" "%targetBase%%%b\%%~nxa"
endlocal
for %%f used to ensure source path will always have an ending backslash
for %%a used to enumerate the files to process from source
for %%b used to split the name in parts and do the move
move command is only echoed to console. If the output is correct, remove echo
I have a good command over cmd commands, but this may require a variable or a loop which is where I fail in batch commands. Please help if you can!
-- Have about 100 subdirectories each has 1-20 HTML files in it. There are about 100 HTML files in the root directory too.
-- Need to replace all HTML files in the above directories with the same HTML source (copy over existing file and keep the name the same). Basically trying to replace all existing files with a redirect script to a new server for direct bookmarked people. We are running a plain webserver without access to server-side redirects so trying to do this just by renaming the files (locked down corp environment).
Seems pretty simple. I can't get it to work with wildcards by copying the same file over to replace. I only get the first file replaced, but the rest of the files will fail. Any one with any advice?
This should do it from the command prompt. Replace % with %% for use in a batch file.
for /r "c:\base\folder" %a in (*.html) do copy /y "d:\redirect.html" "%a"
Without knowing more precisely how you want to update the file content I suggest the following rough approach.
To re-create your example, I had to create some folders. Run this command to do that:
for /l %i in (1,1,20) do mkdir fold%i
I then used this script to create some example files:
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do call :makefiles %%i
goto :EOF
:makefiles
set /a number+=1
touch %1\file%number%.txt
echo %number% >%1\file%number%.txt
I then used this script to append the text changed to the file. Not sure if that is what you wanted - probably you need something more sophisticated.
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do #for %%f in ("%%i\*.txt") do call :changetext %%f
goto :EOF
:changetext
echo changing file contents to ^"changed^" for file: %1
echo changed>>%1
I need to convert some xls files into xlsx files. I can successfully convert one xls file into xlsx by running this command into cmd prompt (windows):
ssconvert inputFileName.xls outputFileName.xlsx
(ssconvert is a Gnumeric's command-line utility that can convert between different spreadsheet file formats)
I'd like to write a batch file that FOR EACH file in a specified directory runs the command I wrote above, using the current file name both for input and for output filename.
For example, if I have this set of files:
c:\directory\file1.xls
c:\directory\file2.xls
c:\directory\file3.xls
the output should be
c:\directory\file1.xlsx
c:\directory\file2.xlsx
c:\directory\file3.xlsx
so the batch pseudo code should be something like
directory = c:\directory\
for (fileName in directory)
ssconvert fileName.xls fileName.xlsx
Can anyone help me?
for /r %%v in (*.xls) do ssconvert "%%v" "%%vx"
a couple have people have asked me to explain this, so:
Part 1: for /r %%v in (*.xls)
This part returns an array of files in the current directory that have the xls extension. The %% may look a little curious. This is basically the special % character from command line as used in %PATH% or %TEMP%. To use it in a batch file we need to escape it like so: %%PATH%% or %%TEMP%%. In this case we are simply escaping the temporary variable v, which will hold our array of filenames.
We are using the /r switch to search for files recursively, so any matching files in child folders will also be located.
Part 2: do ssconvert "%%v" "%%vx"
This second part is what will get executed once per matching filename, so if the following files were present in the current folder:
c:\temp\mySheet.xls,
c:\temp\mySheet_yesterday.xls,
c:\temp\mySheet_20160902.xls
the following commands would be executed:
ssconvert "c:\temp\mySheet.xls" "c:\temp\mySheet.xlsx"
ssconvert "c:\temp\mySheet_yesterday.xls" "c:\temp\mySheet_yesterday.xlsx"
ssconvert "c:\temp\mySheet_20160902.xls" "c:\temp\mySheet_20160902.xlsx"
Actually this is pretty easy since Windows Vista. Microsoft added the command FORFILES
in your case
forfiles /p c:\directory /m *.xls /c "cmd /c ssconvert #file #fname.xlsx"
the only weird thing with this command is that forfiles automatically adds double quotes around #file and #fname. but it should work anyway
you can run something like this (paste the code bellow in a .bat, or if you want it to run interractively replace the %% by % :
for %%i in (c:\directory\*.xls) do ssconvert %%i %%i.xlsx
If you can run powershell it will be :
Get-ChildItem -Path c:\directory -filter *.xls | foreach {ssconvert $($_.FullName) $($_.baseName).xlsx }
I am doing similar thing to compile all the c files in a directory.
for iterating files in different directory try this.
set codedirectory=C:\Users\code
for /r %codedirectory% %%i in (*.c) do
( some GCC commands )