Copying files with CMD - cmd

I have one folder in which there is a txt file that generates every 30 minutes and overwrites the previous version of it. I wish to create a backup folder where a copy of the txt file will be held, for that to happen I want a cmd file that basically copies the file and adds the timestamp at the end of the new file (I will even settle for a serial number at the end of the file.) This cmd file I will run using task scheduler of windows.
I tried using the robocopy command but it doesn't seem to create new files but only copy once and keep overwriting over it.
So basically: a command that will copy File.txt -> File_timestamp.txt every 15 minutes without overwriting anything.

Here is a command you can use to make a copy of the file with a timestamp in another directory. When you are confident that the file will be copied correctly, remove the -WhatIf from the Copy-Item cmdlet.
powershell -NoProfile -Command "Move-Item -Path .\yyy.txt -Destination C:\temp\yyy_$(Get-Date -UFormat '%Y-%m-%dT%H-%M') -WhatIf"

Related

Powershell reroute output files of exe into directory

We have an internal process set up in Powershell which runs an exe file internal.exe which creates a lot log files in an absolute path "C:\This\is\absolute" which contains all the log files of the past 30 days. "C:\This\is\absolute" contains also log files from other applications and while internal.exe runs and creates log files in "C:\This\is\absolute" another application might create a log file there as well.
Now we need to send the log files created by internal.exe and for this they have to be moved to another folder "C:\Move\here" after having been created.
The process is currently simply set up as
Start-Process -FilePath "internal.exe"
I was looking for something like
Get-Outputfiles (Start-Process -FilePath "internal.exe") | foreach {Move-Item -Path $_ -Destination "C:\Move\here"}
but I found only ways to write output to files, e.g., via Out-File. Is there a way to get something like the Get-Outputfiles which lists the paths of output files from a process?

How to use short-cut paths to Compress-Archive to zip current folder into same destination

I am using Compress-Archive and want to zip the current directory into the same path. However I do not want to have to type out the entire file path both times. Is there an easy way to do this?
I am using windows 10 pro.
This works for the most part Compress-Archive . test.zip but I want it to be on the same level as the current directory so I need to put it back one spot.
Something like this is what I want:
path/test
path/test.zip
What I am getting:
path/test
path/test/test.zip
It is going inside the actual folder which is not what I want
You propably want that:
Compress-Archive * ..\test.zip
The wildcard * avoids that the name of the folder is put inside the zip.
Using .. for the output path we go one level up in the directory tree.
This command will fail if test.zip already exists. Either add parameter -update to update the archive or add -force to overwrite the archive. Both can be used even if the archive does not already exist.
If the current working directory is "t", it can be included using the following command. I would note that I do not think putting the destination .zip file in the directory being compressed is a good idea.
Compress-Archive -Path $(Get-ChildItem -Recurse -Exclude t.zip) -DestinationPath .\t.zip -Force
It is shorter if you are willing to use aliases and cryptic switches.
Compress-Archive $(gci -r -e t.zip) .\t.zip -Force
If I have misinterpreted your situation, please leave a comment or improve the information provided by editing the question.

how do I find all exe files using command line for windows?

I'm a newbie. I am trying to figure out how to use the command line. Please could you tell me what command I should enter so that I can get a list of all the exe files on my computer. thanks.
You can use the dir functionality to search the directory and all of its children directories while filtering on a particular file type.
dir /s /b *.exe | findstr /v .exe.
Source
If you want to find all the executable files that are on the path and/or in the current directory, i.e., all the files you can run from the command line without specifying a path, this should work:
where *.exe
To get names of all .exe files , that are currently running then type tasklist in cmd.
http://ss64.com/nt/tasklist.html
Here's another method I use a lot for tasks like this.
Open powershell and navigate to your root directory by entering the command
cd c:/
cd stands for change directory, and is an alias for the command "Set-Location". We are setting the location to C:/
Next run the following command:
Get-ChildItem -Filter "*.exe" -Recurse
Get-ChildItem is a function that gets the files and folders in a file system drive, and runs on whatever directory you're current at by default.
-Filter "*.exe" is an argument that specifies to only find filenames which end in ".exe". (The * is a type of regular expression notation).
-Recurse is an argument that specifies to search all child directories. This will make your function run on "C:/", but also all child directories of C:/, and all child directories of those directories and so on. This will allow you to search the entire drive.

Windows batch script to rename and delete files based on extension and time created

We have a program that take specific XML files and imports them, then changes the file from .xml to .tmp. Once it turns to .tmp we can delete the file.
Occasionally what happens is the XML file doesn't import properly, in which case it gets renamed from .xml to .bad and the file has to be manually adjusted (could be an improper xml file or whatever). What we've discovered is that roughly 90% of the time if you rename the file back to .xml it imports fine.
I've created a batch script to run every couple of minutes that automatically deletes the .tmp files. What I also want to do is add something to the script that renames .bad file to .xml in order to let the program try and import it again. That will cover the 90%.
In order to cover the last 10%, I'd like to set it up so that after about 10 minutes, it no longer tries to rename the file .bad back to .xml (based on the creation date).
So far, what I have is the script to delete the .tmp files:
forfiles -p "C:\path\to\xml" -m *.tmp -c "cmd /c del #PATH"
And I have the script to rename .bad to .xml:
forfiles -p "C:\path\to\xml" -m *.bad -c "cmd /c ren *.bad *.xml"
How do I tell the 2nd command to do it only for files whose creation date (not last modified date) was less than 10 minutes ago?
Updated:
Here's the full solution (thanks to jon Z for pointing me in the right direction):
powershell.exe -command "get-childitem 'path\to\xml' -filter *.tmp | Remove-Item"
powershell.exe -command "get-childitem 'path\to\xml' -filter *.bad | where-object {$_.creationtime -gt (get-date).addminutes(-8)} | foreach-object {move-item $_.fullname ($_.fullname-replace '.bad','.xml')}"
If using powershell instead of cmd is an option, you could do the following:
to delete the files:
get-childitem c:\path\to\xml -filter *.tmp | remove-item
to rename the .bad to .xml
get-childitem c:\path\to\xml -filter *.bad | where-object {$_.creationtime -gt (get-date).addminutes(-10)} | foreach-object {move-item $_.fullname ($_.fullname -replace '.bad','.xml')}
Assuming that when a file fails twice it will never succeed, then instead of just renaming the .bad files to .xml you could create a tracking file (let's call it .trk). Next time around delete all the old .trk files and any matching .bad files that are still there (since this means that the file failed twice) before tracking the remaining .bad files.

Renaming a file and remove 'dot' and replace it with' _'

I have set of files in a folder with name like abcd.15678
I want to remove the . and replace it with _
Pls suggest the windows command to do this
This solution is reposted from How to Batch Rename Files in Windows: 4 Ways to Rename Multiple Files by Chris Hoffman
PowerShell offers much more flexibility for renaming files in a command-line environment. Using PowerShell, you can pipe the output of one command – known as a “commandlet” in PowerShell terms — to another command, just like you can on Linux and other UNIX-like systems.
First of all, open Powershell ISE and then navigate to the directory (folder) that has the files and folders you'd like to rename by using this command:
cd "C:\your\directory\"
The two important commands you’ll need are Dir, which lists the files in the current directory, and Rename-Item, which renames an item (a file, in this case). Pipe the output of Dir to Rename-Item and you’re in business.
After you launch PowerShell ISE, use the cd command to enter the directory containing your files. You should put the files in their own directory so you don’t accidentally rename other files.
For example, let’s say we don’t want the dot character in our file names – we’d rather have an underscore instead.
The following command lists the files in the current directory and pipes the list to Rename-Item. Rename-Item replaces each dot character with an underscore.
Dir | Rename-Item –NewName { $_.name –replace ".","_" }
Consult Microsoft’s documentation on the Rename-Item commandlet if you want help performing other, more advanced operations.
There isn't a windows command to do this. You should consider writing a script of some sort that obtains a directory listing and enumerates through each entry: changes the dot to an underscore, and calls the windows rename command appropriately.
Actually this should work :
Dir | Rename-Item –NewName { $_.Name.Replace(".","_") }

Resources