Deleting a file from many folder

March 2, 2009 at 07:25:01
Specs: Windows XP
How to delete a particular file from many folders.

For example there are many folders in particular location say c:\temp\folder1

in c:\temp there are many folders like folder1,folder2,folder3....
foldern

The particular file be user.csv, which is present in all the folder(folder1,folder2,folder3..folder3).

Is there any script to delete file user.csv from all the mentioned folders, WITHOUT disturbing the structure.


See More: Deleting a file from many folder

Report •


#1
March 2, 2009 at 07:47:00
del /s c:\temp\user.csv


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
March 2, 2009 at 22:40:33
Hi,

Thanks for your reply. It works.

But I do not want to delete latest 1week or 1 month file

Say, I have received user.csv file for last 8 months on daily basis.

I have to keep the latest 1week or 1 month old file, and remove the other old file.


Is this possible?


Report •

#3
March 3, 2009 at 02:45:57
Nobody I know of has managed 'date math'.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
March 3, 2009 at 03:57:41
Hi,

Thanks for your reply.

Is there any solution for this?


Report •

#5
March 3, 2009 at 07:37:52
Can you create the script using below forfiles syntax.

I tried but did not worked out..

[
I want to delete old user.csv file.
The script should not delete latest 1 month old user.csv file

Say I want to delete user.csv file from jan 2008 to jan 2009.

Feb 2009 user.csv file should not be deleted.

The folder structure is same as discribed above.]

http://webtools.live2support.com/wi...

Syntax:-
forfiles /s /m *.* /d -365 /c "cmd /c echo @file is at least one year old."


Report •

#6
March 3, 2009 at 08:10:07
here is a script to get date difference:
vbdateadd.vbs version
dim d
d = date+wsh.arguments(0)
wsh.echo Year(d)*10000 + month(d) * 100 + day(d)
wsh.quit Year(d)*10000 + month(d) * 100 + day(d)

batdateadd.bat version

@echo off & setlocal enabledelayedexpansion
if "%~1"=="" echo USAGE: %0 [interval] & goto:eof

::get system date format
set f=
for /f "skip=1 tokens=2-4 delims=(./-)" %%a in ('echo.^|date') do (
	set a=%%a
	set b=%%b
	set c=%%c
	for %%x in (yy mm dd) do for %%y in (a b c) do if %%x==!%%y! set f=!f!%%%%y
)

::set datestamp
for /f "tokens=1-3 delims=./- " %%a in ('date/t') do set d=%f%
set/a yy=%d:~0,4%
set/a mm=%d:~4,2%
set/a dd=%d:~6,2% + %1

:dateadd
if %dd% leq  0  set/a mm-=1 & if !mm!==0  set mm=12 & set/a yy-=1
set n=31
for %%a in (4 6 9 11) do if %%a==%mm% set n=30
	   for /f %%b in ('set/a %yy%%%4')   do if not %%b==0 (set d=365) else (
	   for /f %%b in ('set/a %yy%%%100') do if not %%b==0 (set d=366) else (
	   for /f %%b in ('set/a %yy%%%400') do if     %%b==0 (set d=366) else set d=365))
if %mm%==2 set/a n=d-337 

if %dd% gtr %n% set/a mm+=1 & if !mm!==13  set mm=1  & set/a yy+=1
if %dd% gtr %n% set/a dd-=n & goto:dateadd
if %dd% leq  0  set/a dd+=n & goto:dateadd

::echo out the result and set errorlevel on exit
set/a dateadd=%yy%*10000+%mm%*100+%dd%
set dateadd
exit/b %dateadd%

the vbs version is much much more simpler.


Report •

#7
March 4, 2009 at 01:42:17
Could you please edit the below code.

Path from where I have to delete the .csv file "c:\abc"

I have to keep the recent 15 days file and delete the old ones


My Problem is similar to the below mentioned subject:-

HELP Del Files Older Than 15 Days?

::== keep5.bat
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%A in ('dir /b/a-d/o-d') do (
set /a kept+=1
echo !kept!
if !kept! geq 6 call :sub1 %%A
)
goto :eof

:sub1
echo del "%*"
goto :eof
:: DONE


Report •

#8
March 5, 2009 at 01:56:20
Hi,

Can any one help me with the script


Report •

#9
March 5, 2009 at 03:27:32
you lazy, with the above script i gave, you should be able figure out the rest:

anyway, here is a script to search user.csv file(s) older than 15 days:
fileolder.bat

@echo off & setlocal enabledelayedexpansion

call batdateadd -15
set/a stamp=%errorlevel%

::get system date format
set f=
for /f "skip=1 tokens=2-4 delims=(./-)" %%x in ('echo.^|date') do (
	set x=%%x
	set y=%%y
	set z=%%z
	for %%a in (yy mm dd) do for %%b in (x y z) do if %%a==!%%b! set f=!f!%%%%b
)

::search older user.csv file(s)
for /f %%a in ('dir/b/s user.csv') do (
for /f "tokens=1-3* delims=./- " %%x in ("%%~ta") do (
	if %stamp% geq %f% (
		echo OLD: %f% %%a	delete me
	) else (
		echo NEW: %f% %%a	leave me alone
	)
))

you need the above batdateadd.bat or you can opt for vbdateadd.vbs.
there is also julian-gregorian date conversion i posted in another thread which has faster performance for calculating larger day interval.


Report •

#10
March 5, 2009 at 06:46:42
Thanks..

Report •


Ask Question