Use echo to empty file content in Windows Command Prompt - windows

I have a folder on a windows laptop running a program for measuring concrete vibrations that runs 24/7.
The software creates 2 files every 10 minutes *.asx and *.pbx
The *.pbx files becomes 28 MB each.
I run backup from the laptop to an ftp server every night.
I would need to empty the files of its data, tried with echo and that works.
but its thousands of files and i cannot figure out how to do it on all *.pbx extensions.
Cannot remove the files, because then program will then restart the number sequence after 10 min.
I need to keep all files in the directory just need to shrink them.
The data in the files are not important after backup is finished.
I have tried
echo delete >*.pbx and most of the arguments.
Please help!

Use FOR /F - loop command: against the results of another command.
From command prompt:
for /f "delims=" %G in ('dir /B *.pbx') do #copy /Y NUL "%~G">NUL
From a Windows batch script (.bat or .cmd)
#for /f "delims=" %%G in ('dir /B *.pbx') do #copy /Y NUL "%%~G">NUL

To empty a file, you can use copy:
copy /Y nul: myfile.myext

Related

Batch job to delete folder older than 7 days on windows 7

Hi I need to delete folders on my Windows 7 computer which are older than 7 days and have data in it.
I have used the following commands which haven't worked out well for me
FORFILES /S /D -10 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del #path"
The second command which I referred from Batch file to delete files older than N days
also didn't work for me.
Can someone please suggest what to use in the second command on C:\what\ever or suggest an alternative command to delete my folders older than 7 days.
So your task is to delete older backup directories no longer needed.
The tricky solution would be working with "delete older than X days".
The simple solution is following:
#echo off
set "BackupDirectory=C:\Backup"
for /F "skip=7 delims=" %%D in ('dir "%BackupDirectory%" /AD /B /O-D 2^>nul') do (
rd /Q /S "%BackupDirectory%\%%D"
)
The command DIR returns a list which contains
because of /AD (attribute directory) just the subdirectories and
because of /B (bare format) just directory names and
because of /O-D ordered by date with newest at top and oldest last.
The command FOR skips the first 7 lines from list, i.e. the 7 newest subdirectories, and executes on the other (older) directories the command to remove the subdirectory.
In your case with directory names starting with yyyy-mm-dd it would be also possible to use /O-N (ordered reverse by name) instead of /O-D to keep skip=x newest subdirectories and delete all others.
Note: On NTFS partitions the last modification date of a folder changes if any file/folder in this folder is added/modified/deleted, but not on FAT16, FAT32 or exFAT partitions.
On DIR command line after /O-D the option /TC can be added get output the directory list sorted reverse by creation date of the folders. But backup folders are usually not modified later after backup was created. It is a matter of opinion to keep the newest folders taking modifications in the folders into account or rate only folder creation time.
In my experience on deleting backups the date is not so important. Important is only limiting the number of backups to avoid filling a storage media. For example if a backup usually needs 5 GiB, the number of backups to keep might be 10 or 20, but if a backup usually needs only 500 KiB, the number could be increased to 100. The date does not matter, just the total amount of bytes required for the backups, as the limit is the storage media size and not the time span.
And for a log file with lines always appended on each execution of a backup operation, the size of the log file must be usually observed and not the time span on which lines are appended to same log file to avoid that the log file becomes larger and larger. Moving a log file with size > x KiB or MiB to *_old.log with /Y to overwrite an already existing *_old.log and then redirect the new lines into a new log file is quite often the right strategy to have just 2 logs files (*.log and *_old.log) with a defined maximum file size containing log lines of the last x backup operations.
Remove old backup folders older than /d days. So use a minus value.
Oneliner...
forfiles /p y:\foldername /d -50 /c "cmd /c if #isdir==TRUE rmdir #file /s /q"
You want to leave off the forfiles "/s" if you only want to scan the top level folders and once you have that folder, you rmdir the entire folder.
The reason for using rmdir is because if you use del or erase, once it deletes a file, it can change the date on the holding folder. Then the command leaves folders behind.
/p - for path
/d - minus for 50 days old
/c - run a command
#isdir - if folder
Rmdir - better than del or erase
#file - what it was looking for
/s - subfolders
/q - quiet

Windows batch file - process one file at a time

Odd question - but it's driving me a bit crazy. I have a directory where multiple files can be dumped via FTP, then I need to process them one at time. So basically in this directory I could have 1.txt, 2.txt, 3.txt etc then I need to:
copy the file to an archive (if exist copy to archive)
move the file to one specific filename one at a time - data.txt <--- this is what's getting me
run a command on a legacy backend system client using that specific filename (data.txt)
run another command on legacy client using data.txt
delete data.txt
Move on to the next file and repeat
So far I've tried several methods of do loops without any luck - they all get hung up on trying to rename multiple files into one file, and that just kills me. I'd long ago since given up on batch files but annoyingly, this application has to use windows, and Server 2003 to boot.
EDIT: Here's what I've tried-
This works to do one file at a time:
if exist c:\jail\ftp*.txt copy c:\jail\ftp*.txt w:\scans\archive*.txt
if exist c:\jail\ftp*.txt move c:\jail\ftp*.txt w:\data.txt
if exist w:\data.txt C:\temp\rmtcmdb.exe
if exist w:\data.txt del w:\data.txt
I've tried multiple for loops without success, here is the latest (NOTE - I'm just trying to get past the move stage on this one, once I'm done with that I'll add in the rest):
setlocal ENABLEDELAYEDEXPANSION
FOR /f %%a IN ("c:\jail\ftp\") DO (
CALL SET /A x = !x! +1
if !x! == 1 (
CALL copy %%a w:\scans\archive*.txt
CALL move %%a w:\data.txt
)
)
I've also tried some very basic for loops, and again - nothing is getting past the move stage.
Any suggestions?
#echo off
setlocal enableextensions
for %%a in ("c:\jail\ftp*.txt") do (
set "fileName=%%~na"
setlocal enabledelayedexpansion
copy "%%~fa" "w:\scans\!fileName:*ftp=archive!%%~xa"
endlocal
move /y "%%~fa" "w:\data.txt"
start "" /wait "c:\temp\rmtcmdb.exe"
if exist "w:\data.txt" del /s "w:\data.txt" >nul 2>nul
)

batch file to copy a file to multiple computers by iteration?

I have below batch script to copy file from my computer to many computers.
#echo off
xcopy D:\some.txt \\10.124.66.72\texts
xcopy D:\some.txt \\10.294.66.46\testfolder
pause
In the script, i have mentioned all the other computer names/IPs. Now how can i keep other computer names in a separate text file and iterate them in batch file instead writing xcopy command many times? Or is it possible to mention list of computers, iterate through all and use single xcopy command?
Try this:
FOR /F "delims=" %%i IN (targets.txt) DO (
xcopy "D:\some.txt" "%%i"
)
targets.txt should contain entries like "\\10.124.66.72\texts" in each line

Windows 7 desktop.ini shared folder removal script bug

Bit of a batch file question. I imagine lots of people are having fun with shared folders appearing as "My Documents" in windows explorer.
I look after several schools, where we have the pupil home folders set in a folder called say C:\data\pupils\yeargroup...
The teachers then have access to the pupils folder on a share & can review the pupils work. With the onset of Windows 7, whereas before Yeargroup folders have had a list of the pupils names with their mydocs inside, you now see a whole load of folders reporting to be "My Documents" due to the desktop.ini located inside of them.
So, I wrote this little batch file & have been running it in c:\data on a 15 minute automated task which has done a nice job or restoring law & order:
FOR /f "delims=" %%i IN ('dir /s /b /a-d "desktop.ini"') DO attrib -s -h %%i >nul 2>nul
FOR /f "delims=" %%i IN ('dir /s /b /a-d "desktop.ini"') DO del %%i >nul 2>nul
The has worked fine up until deployment at a new site that I've just got involved with. Whereas I set up all folders without spaces:
C:\data\pupils\yeargroupx\joebloggs
the folder names at this site are something like this:
C:\data\pupils\yeargroup x\joe bloggs
I have tested running the commands manually & seem to have to run the following (from within the folder location):
dir /ash
attrib -s -h
del desktop.ini
As there are 100s of users, I obviously want to automate this. Any ideas as to how I would tweak my script? I want to do dir /ash for every folder before I change the attribute of the ini file, otherwise cmd cannot see it.
I got that script running through fudging about rather than any in depth knowledge, so some assistance would be greatly appreciated?
Use "%%i" in the command tails and then spaces and & will not be a problem.

How to copy a location to a batch file from a file that is dragged and dropped to open the batch file

So I've been trying to create a batch file for a piece of software called DiscEX the software requires command line use from cmd.exe windows xp or higher the way it's initiated is like this discex (any arguments needed) location of iso file.
Now I can get the software to run using the batch file but I can't seem to figure out how to copy the target location of a file that was dragged onto it to open the batch file up
Here is what the batch file in notepad looks like.
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex
pause
also I need to be able to start in the working directory of a portable hard drive.
All you need to do is
C:\windows\system32\discex "%1"
to get a file path argument passed into the batch
If the batch file is in the working directory already, put
cd /d %~dp0 in the batch after #echo off
If you want to determine what drive is the external usb drive, use
#echo off
setlocal
set wmi='wmic logicaldisk where "volumeserialnumber='32A78F3B'" get caption'
for /f "skip=1 delims=" %%A in (%wmi%) do (
for /f "tokens=1 delims=:" %%B in ("%%A") do (set drive=%%B)
)
echo %drive%
where volumeserialnumber is the output from vol [driveletter of USB drive:] with the - removed.
When you drag a file on a batch file, the full file path is available in the first argument (%1) of the batch file. If you need this argument to be fed to the discex application as its first argument, you can do:
#echo off
echo Welcome to AutoDiscEx
pause
C:\windows\system32\discex %1
pause

Resources