Move all files created today from network location to local drive - windows

Using my below given script, I simply wanted to cut-paste all the .dat files created today from source to destination, where source is a network path.
#echo off
set datetime=%date:~0,2%-%date:~3,2%-%date:~6,4%_%time:~0,2%-%time:~3,2%
mkdir "D:\data\Backup\%datetime%"
net use L: \\10.xx.xx.xxx\shared\files /persistent:no
set source=L: \\10.xx.xx.xxx\shared\files
forfiles /P "%source%" /M *.dat /D +0 /C "cmd /c move #path D:\data\Backup\%datetime%"
net use L: /delete /y
BUT..its throwing ERROR: The directory name is invalid.
I am not getting why FORFILESis not accepting my network path as a source.
Please can somebody help me out here ?
I have no issue in using powershell as well.

In PowerShell:
$src = '\\10.xx.xx.xxx\shared\files'
$dst = "D:\Data\Backup\$(Get-Date -f 'yyyyMMdd')"
mkdir $dst
Get-ChildItem $src -File | Where {$_.LastWriteTime -gt (Get-Date).Date} | Copy-Item $dst

Related

How to rename text files within subfolders or zip files

I have multiple zip folders with unique names but the files within the folders all have the same name. I need to rename the text files to match the folder names. I also need those renamed text files to be moved to the working directory.
For example, I have the following:
name1.zip decompresses to name1\AncestryDNA.txt but need to be name1.txt.
name2.zip decompresses to name2\AncestryDNA.txt but need to be name2.txt.
I used 7-Zip 19.00 to unzip the files. I decompressed the files because I also need to remove lines starting with "#" from each decompressed text file for use in another program.
I start with a windows batch file the runs the following on my Data directory that contains my zip files:
echo Unzipping the files...
forfiles /p Data /m *.zip /c "cmd /c 7z x *.zip -o* -y"
echo Moving zip files to storage...
forfiles /p Data /m *.zip /c "cmd /c move #file ..\StorageZipFiles"
echo Trimming all except header from AncestryDNA text files...
forfiles /p Data /s /m *.txt /c "cmd /c sed -i '/#/d' #file"
These work but I can't get the rename function to work.
I tried to rename the files before decompressing...
forfiles /p Data /s /m *.zip /c "cmd /c 7z rn #file #fname\AncestryDNA.txt #fname\#fname.txt"
and...
forfiles /p Data /s /m *.zip /c "cmd /c 7z a rn #file #fname\AncestryDNA.txt #fname\#fname.txt"
and after decompressing...
forfiles /p Data /s /m *.txt /c "cmd /c ren #file #fname.txt"
but I have not been successful with carrying the name of the folder over to the text file within the folder.
When trying to rename the files after decompressing, the command prompt does not produce an error message. It keeps the old file name of AncestryDNA.txt nested within the folders name1, name2, etc.
Another way of renaming the files after decompression is this powershell script.
Get-ChildItem -recurse -path Data *.txt | rename-item -path {$_.fullname} -newname {$($_ | split-path | split-path -leaf ) + ".txt"} -whatif
This has two interleaved powershell pipelines, one gets all the right directories and the second pipeline extracts the name of the folder used to rename the file.
Note that I put a -whatif at the end of this for safety. If the output looks good then remove the -whatif to actually change the filenames. It is still best to test this on test data before using on real data.

Trouble moving files in PowerShell using forfiles to a folder whose name has more than one word

I'm new to PowerShell, and am trying to move a few excel spreadsheets into a folder titled "old folder" using the forfiles command. PowerShell says that the files have been moved, but when I check the folder they're not there. They aren't in their original location either.
forfiles /P C:\Users\NewUser\Desktop /M *.xlsx /D +27/05/2019 /C 'cmd /c move #file C:\Users\NewUser\Desktop\"old folder"\'
I tried changing the folder name to a single word "oldfolder" and it worked just fine. What am I doing wrong?
There seems to be a problem with the syntax of your /C parameter. As far as I can tell the argument should be enclosed in double-quotes. And because of the space in the file name that should also be enclosed in double-quotes. Quotes within quotes should be escaped with backslash.
I would suggest this:
forfiles /P C:\Users\NewUser\Desktop /M *.xlsx /D +27/05/2019 /C "cmd /c move #file \"C:\Users\NewUser\Desktop\old folder\""
As forfiles has it's idiosyncrasies even in cmd, I wouldn't drag that along to PowerShell.
IIUR that translates to:
Get-ChildItem 'C:\Users\NewUser\Desktop\*.xlsx' -File |
Where-Object LastWriteTime -ge [DateTime]::Today |
Move-Item -Destination 'C:\Users\NewUser\Desktop\old folder\'
Not sure if you meant that to be /D -27/05/2019 => -lt or -le instead of -ge

Delete folders having particular naming pattern in a path in windows

I have a directory that contains .exe executable and required input files.
For each individual request, a separate directory (of the format Output_) is created in the same path and the respective output files are directed there.
Now i am in need of purging the older output directories. Can somebody explain how to achieve this in windows.
I went through the forfiles documentation and other stack overflow answers but did not find any information on providing name pattern for folders that need to be deleted although this option is available for file deletion.
I am trying the below code. But i want to specify the folder pattern as well so that i do not delete other files.
forfiles /p "C:\work\Analysis\" /d +7 /c "cmd /c IF #isdir == TRUE rd /S /Q #path"
Here an example in Powershell
$Now = Get-Date
$Hours = "4"
$TargetFolder = **!Folder in which old data exists!**
$LastWrite = $Now.AddHours(-$Hours)
$Folders = get-childitem -path $TargetFolder |
Where {$_.psIsContainer -eq $true} |
Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($Folder in $Folders)
{
write-host "Deleting $Folder" -foregroundcolor "Red"
Remove-Item $TARGETFOLDER\$Folder -recurse -Force
}
Below command did the job for me. /M works with directories as well. Thanks to JJS's answer on https://superuser.com/q/764348
forfiles /P C:\work\Analysis\ /M guid_* /d +5 /C "cmd /c if #isdir==TRUE rmdir /s /q #file"

how to find large files in command line windows for version 5.1

How do I find large files in windows ver 5.1 command line?
For Windows ver 6.1, I can run the following command:
forfiles /p c:\ /s /m . /c "cmd /c if #fsize gtr 100000 echo #file
#fsize"
However what is the equivalent command for version 5.1 windows?
Appreciate your quick help!
(added quote marks)
To be run from command line
for /r c:\ %f in (*) do if %~zf gtr 100000 echo %f %~zf
To run it from batch file, change % with %%
EDIT - As stated in the comments, arithmetic in batch command lines have some limits on the operands. In this case, the %~zf (size of file referenced in for command) in the left part of the if command have no problems, but the value on the right size is limited to values from -2147483647 to 2147483646.
To handle it, if command is executed under an administrator account, wmic command can be used. The command in OP question can be written as
wmic datafile where "drive='c:' AND filesize>'100000'" get caption, filesize
first search biggest directories in actual directory
diruse /* /m /c /, . |sort
cd to the chosen dir; you can use diruse again or
search biggest files in the chosen directory, which are more than 500MB for example:
forfiles /s /c "cmd /c if #fsize gtr 500000000 echo #path #fsize bytes"
For windows, I couldn't find one answer that suits me for command line, so i coded my answer in powershell.
Following code will help you find the list of files (more than 1 KB) in your desired path to scan.
#Run via Powershell
#$PathToScan refers to the directory where you want to find your files greater than 1 KB.
$PathToScan="E:\"
"Path to scan:`t $PathToScan"
$hash = $null
$hash = #{}
Get-ChildItem -path $PathToScan -recurse | Where-Object {
#If you want to find files greater than 1GB, replace all KB in this powershell script to GB.
($_.Length /1KB) -gt 2
} | ForEach-Object {
$hash.add(
$_.DirectoryName.Replace("$PathToScan","")+$_.Name,
[math]::Round(($_.length/1KB),0))
}
$TableStyle = #{Expression={$_.Value}; Label="Size (KB)"; Width=10; Alignment = 'Left'},
#{Expression={$_.Name}; Label="Name"; Width=100}
$hash.GetEnumerator() | Sort Value -Descending| Format-Table -Property $TableStyle
Countdown 5
Function Countdown{
Param ($TimeInSec)
Do {
Write-Host "$TimeInSec"
Start-Sleep -s 1
$TimeInSec--
}
Until ($TimeInSec -eq 0)
}
Or you may view my code here;
[Find large files in KB for Windows]:
https://github.com/worldpeacez0991/powershell
Hope my answer helps someone, have a great day ^ ^

Moving All Files with a Certain Name (Sorting) WINDOWS 8

Ok, so I have a large amount of ebooks and other files that I would like to sort into folders by Author name. The way I would normally do this in Linux will not work on my Windows 8 laptop. I've been using Powershell, but if there is a better way in Python or by using a bash script I am willing to use whichever can get this done the easiest way. I need it to work recursively because there are many subfolders, (a lot are sorted by genre). What I have tried so far is:
mkdir 'C:\Users\Amanda\Downloads\BOOKS\Peter Straub'
Copy-Item 'C:\Users\Amanda\Downloads\*Peter Straub*' -recurse `
'C:\Users\Amanda\Downloads\BOOKS\Peter Straub'
Unfortunately for me, that only looks for directories named peter straub. And if i change it to ~\Downloads*Peter Straub*.* it just doesn't work. I tried using the -match and -contains but they failed to work as well. I am relatively new to scripting in windows 8 and would really appreciate anyones help. I would like to implement the read-host -prompt so I don't have to edit the script everytime i run it. And I would like it to actually MOVE the file not just copy but Move-Item doesn't have a recurse option. So could I somehow have the script copy and then delete the original. I tried this also:
$author = (read-host -prompt 'Enter Name')
mkdir 'C:\Users\Amanda\Downloads\BOOKS\$author'
Copy-Item 'C:\Users\Amanda\Downloads\*$author*.*' -Recurse | Remove-Item
But this also did not work. I would love to get something like this to work in an automated batch.
So basically I want to pick an authors name, make a new directory using that authors name, then search and move all files containing that authors name into the new directory.
EDIT::::
So basically I combined the suggestions of the three answers and what I got works PERFECTLY! Here is the .bat that worked:
#echo off &setlocal
set /p "Author=Enter Name: "
md "%author%"
for /f "delims==" %%i in ('dir C:\Users\Amanda\Downloads /b /s^|find "%author%" /i') do (move "%%i" "C:\Users\Amanda\Downloads\BOOKS\%author%")
EDIT AGAIN:
So I tried the revised one to try and get it to accept spaces:
#echo off &setlocal
set /p "Author=Please enter an Author: "
md "C:\Users\Amanda\Downloads\BOOKS\%author%"
for /f "delims==" %%i in ('dir C:\Users\Amanda\Downloads /b /s^|find "%author: =.%" /i') do (move "%%i" "C:\Users\Amanda\Downloads\BOOKS\%author%")
It works correctly in making the directory but it does not move the files.
set /p author=Please enter an Author:
md "C:\Users\Amanda\Downloads\BOOKS\%author%"
for /f "delims==" %%i in ('dir C:\Users\Amanda\Downloads /b /s^|findstr "%author: =*%"') do (move "%%i" "C:\Users\Amanda\Downloads\BOOKS\%author%"
this should work. if not, please comment so I can fix it.
Try this:
$author = (read-host -prompt 'Enter Name')
$basedir = 'C:\Users\Amanda\Downloads'
$destdir = "$basedir\BOOKS\$author"
New-Item $destdir -Type directory
Get-ChildItem $basedir -Recurse | ? {
-not $_.PSIsContainer -and
$_.Name -like "*$author*" -and
$_.Directory.FullName -ne $destdir
} | % {
Copy-Item $_.FullName "$destdir\"
Remove-Item $_.FullName
}
#echo off &setlocal
set /p "author=Enter the author's name: "
cd /d "C:\Users\Amanda\Downloads"
md "books\%author%"
for /r %%a in (*"%author%"*) do move "%%~fa" "books\%author%"

Resources