How to select and move list of files without explicitly selecting them individually - powershell

I have 300 pdf files in a folder in Windows10. The pdf files are named as "1.pdf","2.pdf","3.pdf"......"300.pdf".
I also have a list of 50 random file names(all file names are between 1 and 300) in txt file such as "2.pdf","13.pdf",....
I want to select the specified files mentioned in txt file from the folder and move it to another folder. Is there a way to do it quickly and at once without selecting each individual file and moving it

In Powershell you could do something like
gc txtfile.txt | %{ move $_ destination }
... which would gc or get the contents of txtfile.txt, then for each line, move linefromtxtfile to destination.

The FOR command with the /F option can be used to read a file. Then you can use the move command to move it to the destination directory.
FOR /F "usebackq delims=" %%G IN ("myfile.txt") DO MOVE "%%~G" "destination"
The USEBACKQ option is needed if your text file name has spaces or special characters in it. The DELIMS option is needed so that it does not tokenize the data inside the text file.
This also assumes that your PDF files exist in the same folder as the batch file or the text file lists the relative or absolute paths to the files.
Destination would be an absolute path or relative path depending on your needs.

Related

Compare file names (minus extension) in different directories and copy/move if the same name with AutoHotkey

I want to compare filenames (minus extensions) between two directories and if there are matches, copy (or move [tbd]) the files from one of two to a 3rd directory. i.e.
Dir_A has a.jpg, b.jpg, c.jpg, d.jpg, e.jpg, f.jpg
Dir_B has a.pdf, c.pdf, d.pdf, f.pdf
results would be
Dir_C gets a.jpg, c.jpg, d.jpg,f.jpg
I've been able to accomplish this with a batch file, but want to learn how to through AHK.
The batch file is:
#Echo Off & SetLocal EnableExtensions
pushd D:\temp
For /F "tokens=*" %%I IN ('dir /a-d /b *.jpg') DO (
IF EXIST "D:\temp\comp\%%~nI.pdf" move "%%~I" "D:\temp\new\"
)
After a lot of looking, finding similar posts and attempting to interpolate, I think I'm close but am obviously missing something. I'm hoping someone would please shed light on this for me.
#NoEnv
SendMode Input
SFolder:="D:\temp\" ;Source folder
CFolder:="D:\temp\comp" ;Compare folder
DestDir:="D:\temp\new" ;where to move files
Loop,
{
Loop, %SFolder%*.jpg ;look for all jpg files
JpgName = %A_LoopFileName% ;save the file names to var
Loop, %CFolder%*.pdf ;look for all pdf files
PdfName = %A_LoopFileName% ;save the file names to var
JpgCompare:=Trim(JpgName,".jpg") ;remove the files .ext
PdfCompare:=Trim(PdfName,".pdf") ;remove the files .ext
If JpgCompare = %PdfCompare% ;if there are matching file names (minus .ext)
;in both directories
{
FileMove, %JpgName%, %DestDir% ;move the file.jpg to the "new" directory
}
Else
{}
}
Esc::
ExitApp
You can use SplitPath to store the jpg file name without its path, dot and extension in a variable (name_no_ext) and check for the existence of a pdf file with the same name in the other directory using FileExist():
SFolder:="D:\temp\" ;Source folder
CFolder:="D:\temp\comp" ;Compare folder
DestDir:="D:\temp\new" ;where to move files
Loop Files, %SFolder%*.jpg ;look for all jpg files
{
SplitPath, A_LoopFileName,,,, name_no_ext
If FileExist(CFolder . "\" . name_no_ext . .pdf)
FileMove, %A_LoopFileFullPath%, %DestDir% ;move the file.jpg to the "new" directory
}

Find and replace text between certain characters with filename

Find and replace text between certain characters with filename, have about 100 files that require this so I'm thinking it needs a occurrence or something.
Original file name: test1.txt
Inside file replace the following two lines with the file name (inside each file the below strings might not be right after each other):
location000:/computer/[project]/name/123.php,32,1,2,512,0,,txt
newlocation000:/computer/[project]/name/123.php,32,1,2,512,0,,txt
Output in file test1.txt
location000:/computer/[project]/name/test1.php,32,1,2,512,0,,txt
newlocation000:/computer/[project]/name/test1.php,32,1,2,512,0,,txt
This is an easy to achieve task with using JREPL.BAT written by Dave Benham which is a batch file / JScript hybrid to run a regular expression replace on a file using JScript.
#echo off
if not exist "%~dp0jrepl.bat" (
echo ERROR: JREPL.BAT missing in directory "%~dp0".
echo/
pause
goto :EOF
)
for %%I in ("C:\Temp\*.txt") do call "%~dp0jrepl.bat" "^((?:new)?location000:/.+/).*(\.[^.,]+,)" "$1%%~nI$2" /F "%%I" /O -
This batch file works only on NTFS drives. It can result in an endless running loop on FAT16, FAT32 or ExFAT drives or skipping some text files. For a working solution independent on file system replace the last command line by:
for /F "eol=| delims=" %%I in ('dir "C:\Temp\*.txt" /A-D-H /B /ON 2^>nul') do call "%~dp0jrepl.bat" "^((?:new)?location000:/.+/).*(\.[^.,]+,)" "$1%%~nI$2" /F "C:\Temp\%%I" /O -
The batch file JREPL.BAT must be stored in same directory as the batch file with the code above. For that reason the batch file checks first if JREPL.BAT really exists in directory of the batch file and if this is not the case, outputs an error message, halts script execution to make it possible for a user to read that error message and then exits. See Where does GOTO :EOF return to?
The command FOR searches in specified directory C:\Temp for non hidden files matching the wildcard pattern *.txt and calls for each found text file the batch file JREPL.BAT to replace the file name between last / and first string starting with ., having one or more characters not being a dot or a comma with next character being a , (= file extension and comma) on lines starting case-sensitive with location000:/ or newlocation000:/ by the file name of the current *.txt file without file extension. So a file name in existing file can contain also one or more . in file name before file extension.
The solution working also on FAT drives uses command DIR to get a captured list of *.txt files with just file name and file extension without path and FOR processes this file names list line by line, i.e. file name by file name. So the list of *.txt files to process does not change on running FOR calling JREPL.BAT as it would be the case on using FOR directly to find the *.txt files on FAT drives.
NTFS returns a list of directory entries matching a wildcard pattern sorted alphabetically and so the list of *.txt files does not modify during FOR iterations in this case. But all FAT file systems return the list of directory entries matching a wildcard pattern according to last modification in directory with last modified file at end of the list. So while FOR gets one file name after the other on using for %%I in ("C:\Temp\*.txt") do from file system and processes the file with calling jrepl.bat which modifies the file, the file list changes on FAT drives and next directory entry returned to FOR on its search for *.txt is either the file just modified (= endless loop) or another file after skipping a file which should be also modified because of file list changed since last directory access by FOR.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains also %~dp0 ... drive and path of argument 0 being the batch file itself.
dir /?
echo /?
for /?
goto /?
if /?
pause /?
jrepl.bat /?

Strip image metadata and rename it using a text file as source of input

I am ok with PowerShell or cmd-based suggestions. This is what I am trying to do:
I have this folder structure:
C:\Parent\Child01\ - Lots of JPG images
C:\Parent\Child02\ - Lots of JPG images
C:\Parent\Child03\ - Lots of JPG images
so on and so forth.
What I am doing currently manually:
I will be inside the Child 01 folder and run this command to strip meta data from all images
exif -r -all= -ext jpg -ext gif -ext png
I have a txt file with keywords one per line. I want the images inside my current directory (Child01) to be replace with the names I gave in the text file.
How I am doing this right now is by using an excel sheet with 3 columns :
Original File Name | New File Name | Rename Command
Original File Name has the content of all files names (I get this by running dir /b /a-d)
New File Name has my keywords
The Rename Command is a formula =concatenate("ren ",A2,".jpg ",B2, ".jpg")
This helps me generate the formula which I copy paste from command line to bulk rename.
Can some help me with a batch file or powershell script so I can get it all done in one go, please?
No need to bother Excel. It renames each .JPG to the next name of the file (it should just contain one column ("NewFileName" (without extension)). If it runs out of either files or lines, it stops.
#echo off
setlocal enabledelayedexpansion
<file.txt (
for /f "delims=:" %%a in ('dir /b *.jpg') do (
set "x="
set /p x=
if not defined x goto :eof
ECHO ren "%%a" "!x!%%~xa"
)
)
Check the output before you remove the ECHO command to actually enable the ren command.

Batch File to move files with the same string of characters into another folder with that string name

I am using Windows 7 Enterprise and have approximately 20 files per day for the last 365 days that I need to sort.
All of the files are in the same directory. Each file name also contains the date of the file's creation. The date is in the format MM-DD-YYY and starts at the 29th character of the file name. The files have the .csv extension.
I need to create a batch file to move all of the files with the same date into their own folder and onto another drive on my computer.
#echo off
setlocal EnableDelayedExpansion
for %%a in (*.csv) do (
set fileName=%%~a
set datePart=!fileName:~28,9!
if not exist "D:\!datePart!" md "D:\!datePart!"
move "%%a" "D:\!datePart!"
)
This script extracts the date part of each file name and uses it as the name of the folder to move the file to. If the folder doesn't exist, it is created, then the file is moved to it.
As written, the script iterates over the .csv files in the current directory. This is specified by the mask in the for loop: *.csv. You can change the mask to include a specific path to process, like C:\path\to\*.csv.
The target drive is also hard-coded and assumed to be D:. Change the corresponding entries of the script if you need to use a different drive.
Detail information on every command used in this script can be obtained by calling the command's built-in help from the command prompt, using either of the below syntaxes:
command /?
help command

How to use xcopy to select specific folders(Not Files) within another folder

I am trying to xcopy specific folders from some directory to another folder, problem is i don't want to copy all the folders but i want specific folders. E.g. DirectorySource has folders (folderA,folderB,folderC) but i want to copy folderA and folderB only to DirectoryDestination.
Create a text file, say C:\excludes.txt with the following contents:
DirectorySource\folderC\
then you can copy:
xcopy DirectorySource DirectoryDestination /s /i /exclude:C:\excludes.txt
The file excludes.txt contains a list of strings, one per line. If a file or directory matches that string, then it will not be copied. If you want to exclude folder, it is safer to use the above string instead of simply folderC, which might skip a file called folderC_listing.txt