Adding/Deleting File script

L-Plate
L-Plate used Ask the Experts™
on
I have the following script integrated into a batch file which i plan to run via group policy.

copy \\server1\users\cisco.pcf "C:\Program Files (x86)\Cisco Systems\VPN Client\Profiles"

I plan to deploy this when a user logs on but I need to the script to do the following as well :

1) Delete the old cisco.pcf file
2) Create a test condition so that the deletion/adding of files is not repeated when the user logs on a second time.

Any ideas
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Here's a VBScript.  It'll write a blank file with no extension to the Profiles folder called 'copied' after it runs.  If the script sees the file the next time it is run, it will not copy it again.

Just change the server path on Line 6
Set oFSO = CreateObject("Scripting.FileSystemObject")
FilePath = "C:\Program Files (x86)\Cisco Systems\VPN Client\Profiles\"
If Not oFSO.FileExists FilePath & "copied"
	oFSO.OpenTextFile FilePath & "copied"
	If oFSO.FileExists FilePath & "cisco.pcf" Then oFSO.DeleteFile FilePath & "cisco.pcf"
	oFSO.CopyFile "\\server1\users\cisco.pcf", FilePath & "cisco.pcf"
End If

Open in new window

Commented:
Two typos above, sorry.
Set oFSO = CreateObject("Scripting.FileSystemObject")
FilePath = "C:\Program Files (x86)\Cisco Systems\VPN Client\Profiles\"
If Not oFSO.FileExists FilePath & "copied" Then
	oFSO.CreateTextFile FilePath & "copied"
	If oFSO.FileExists FilePath & "cisco.pcf" Then oFSO.DeleteFile FilePath & "cisco.pcf"
	oFSO.CopyFile "\\server1\users\cisco.pcf", FilePath & "cisco.pcf"
End If

Open in new window

Commented:
One thing I would say is unless all your kit is 64 bit then use %programfiles% to point to the local program files dir, whether that is Program files (x86) on a 64 bit machine, or Progam files on 32 bit?

Would suggest just using a single xcopy with the /d switch.  This will copy the file only if it is newer - assuming the cisco.pcf file doesn't change on the users's PC at all?

xcopy /d /y \\server1\users\cisco.pcf "%programfiles%\Cisco Systems\VPN Client\Profiles"

or if does change then add a flag file to check if it has been done, e.g.

set directory=%programfiles%\Cisco Systems\VPN Client\Profiles
set flagfile=V2
if exist "%directory%\%flagfile%" (
  echo Nothing to update - already has %flagfile% script
) ELSE (
  echo Copying new cisco.pcffile for %flagfile%
  copy \\server1\users\cisco.pcf %directory%"
  echo Installed %date% %time% by %username% on %computername% > "%directory%\%flagfile%"
)

1. Uses a variable to store the directory to save typing it multiple times.
2. Sets a filename of a flag file to drop on the same dir to say it has been dome
3. Checks if the flag file exists.
4. if it does then just display a message saying it has already been done - remove if you wish.
5. otherwise display that it is being copied, copy the file overwriting one if it is already there
6. Drop a flag file with date / time / username / computername in it so you can see who ran the copy if needed.

Steve
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Author

Commented:
Thanks guys, sorry I should have been  a little clearer on this

The current config file called Remote.pcf, it needs to be deleted and then replaced it with Cisco.pcf. This must be done once only to prevent repetition.

Can you update you code to reflect the config names ?

Commented:
Try this:

Set oFSO = CreateObject("Scripting.FileSystemObject")
FilePath = "C:\Program Files (x86)\Cisco Systems\VPN Client\Profiles\"
If oFSO.FileExists(FilePath & "Remote.pcf") Then oFSO.DeleteFile(FilePath & "Remote.pcf")
If Not oFSO.FileExists(FilePath & "cisco.pcf") Then oFSO.CopyFile "\\server1\users\cisco.pcf", FilePath & "cisco.pcf"

Open in new window

Commented:
OK, sorry do you mean :

#1 if remote.pcf exists on c: then delete it and copy cisco.pcf to cisco.pcf
or
#2 if remote.pcf exists on c: then delete it and copy cisco.pcf to remote.pcf

Assuming it is #1 then just this will do:

set directory=%programfiles%\Cisco Systems\VPN Client\Profiles
if exist "%directory%\remote.pcf" (
  del "%directory%\remote.pcf
  copy \\server1\users\cisco.pcf %directory%"
)

As you don't need to keep track of it being copied you just need to check if remote.pcf exists.  If it does then delete it and copy the cisco.pcf file

If it is #2 then you can't tell whether it was copied or not so we'd need to use a flag file again.

set directory=%programfiles%\Cisco Systems\VPN Client\Profiles
set flagfile=V2
if exist "%directory%\%flagfile%" (
  echo Nothing to update - already has %flagfile% script
) ELSE (
  echo Copying new cisco.pcf file for %flagfile%
  if exist "%directory%\remote.pcf" del "%directory%\remote.pcf"
  copy \\server1\users\cisco.pcf %directory%\remote.pcf"
  echo Installed %date% %time% by %username% on %computername% > "%directory%\%flagfile%"
)


Steve

Author

Commented:
hi guys

Sorry to be a pain

My script requirements have taken a slight change.

The script needs to do the following now :

1) Copy two new config files  config1 and config 2 to C;\Program files
2) The old config file must not be deleted and must remain
3) The two new config files must be only written once to the location, not twice.

Can you help ? I will allocate points right away once this works !!

thanks

Commented:
OK try this:

set directory=%programfiles%\Cisco Systems\VPN Client\Profiles
set flagfile=V2
if exist "%directory%\%flagfile%" (
  echo Nothing to update - already has %flagfile% script
) ELSE (
  echo Copying new cisco pcf files for %flagfile%
  copy \\server1\users\cisco1.pcf %directory%"
  copy \\server1\users\cisco2.pcf %directory%"
 
 echo Installed %date% %time% by %username% on %computername% > "%directory%\%flagfile%"
)

Commented:
And if you choose the VBScript route:


Set oFSO = CreateObject("Scripting.FileSystemObject")
FilePath = "C:\Program Files\Cisco Systems\VPN Client\Profiles\"
If Not oFSO.FileExists(FilePath & "config1.pcf") Then oFSO.CopyFile "\\server1\users\config1.pcf", FilePath & "config1.pcf"
If Not oFSO.FileExists(FilePath & "config2.pcf") Then oFSO.CopyFile "\\server1\users\config2.pcf", FilePath & "config2.pcf"

Open in new window

Author

Commented:
Josika


I'm getting an error

Line 3
Character 58
Path not found

The UNC has been checked and it works fine, any suggestions ?

Commented:
Verify the file exists at the exact UNC path you have entered.  Also, make sure the user account you are running the script under has access to the share where the file is.

Commented:
I imagine it it because this is Program Files vs Program Files (x86) if running on 64 bit Windows?  

Have you tried the batch file approach?  It could simplify if you wish.  At the moment it drops a flag file to day it has copied the files.  It could just check if the files exist, but then if they needed updating you would need to do it a different way again.  With mine if you need to update them again just change them on your file server and change the version to "V3" or ehatever.


Steve

Author

Commented:
Thanks dragon it/josika

Josika

Just another quick ammendment. How do i get this script to run on 32bit machines as well as 64bit ? I imagine an error will be thrown up when c:\program files (64) is only present and not c:\progam files

thanks

Commented:
use the batch file, it already does!

Commented:
This will check the Processor for x86 (32-bit processor) and will adjust the filepath accordingly.


Set oFSO = CreateObject("Scripting.FileSystemObject")
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery("SELECT * FROM Win32_Processor")
For Each objProcessor In colSettings
	If InStr(LCase(objProcessor.Description), "x86") > 0 Then
		FilePath = "C:\Program Files\Cisco Systems\VPN Client\Profiles\"
	Else
		FilePath = "C:\Program Files (x86)\Cisco Systems\VPN Client\Profiles\"
	End If
Next
If Not oFSO.FileExists(FilePath & "config1.pcf") Then oFSO.CopyFile "\\server1\users\config1.pcf", FilePath & "config1.pcf"
If Not oFSO.FileExists(FilePath & "config2.pcf") Then oFSO.CopyFile "\\server1\users\config2.pcf", FilePath & "config2.pcf"

Open in new window

Author

Commented:
Josika

The script works fine on x64 machines but throws up an error on x86.

The error is as follows :

Line 12
Character 58
Path not found

I have double checked the user can access the share where the config is and has permissions to run script under thier login

Author

Commented:
Josika

A colleague of mine said you are quering the processor architecture instead of the OS version. I think we are nearly there, are you going to respond so I can award the points.

Commented:
You said that you wanted this as part of an existing batch script.... I've made you a batch script that works to put into the program files or program files (x86) dir and you can just copy into the middle of your existing batch script.... so have you tried it?

Author

Commented:
Ok Dragon IT

Do the areas in between % need populating ?

Commented:
No, it will work as it stands.  %programfiles% is an environment variable for this purpose.  It resolves to c:\program files or c:\program files (x86) or whichever drive the program files dir. is on.
I set directory variable to make it easier to change if needed based on the above environment variable.

Give it a try and let me know if any issues.

Author

Commented:
Dragon


I'm getting the following message in DOS

Copying new cisco pcf files for V2
The syntax of the command is incorrect.
The syntax of the command is incorrect.
The system cannot find the path specified.
Commented:
ok, might need to mod. there sorry.

I can see there was a quote missed from the two copy lines in mycopy/pasting though, sorry.  They should be:

  copy \\server1\users\cisco1.pcf "%directory%"
  copy \\server1\users\cisco2.pcf "%directory%"

and here is re-arranged:

@echo off
set directory=%programfiles%\Cisco Systems\VPN Client\Profiles
if exist "%programfiles(x86)%\Cisco Systems\VPN Client\Profiles" SET directory=%programfiles(x86)%\Cisco Systems\VPN Client\Profiles
if not exist "%directory%" (
  echo Directory does not exist, press any key to exit
  pause
  exit /b
)
set flagfile=V2
if exist "%directory%\%flagfile%" (
  echo Nothing to update - already has %flagfile% script
) ELSE (
  echo Copying new cisco pcf files for %flagfile%
  copy \\server1\users\cisco1.pcf "%directory%"
  copy \\server1\users\cisco2.pcf "%directory%"
 
 echo Installed %date% %time% by %username% on %computername% > "%directory%\%flagfile%"
)


You should get on the first try:

C:\>cp
Copying new cisco pcf files for V2
        1 file(s) copied.
        1 file(s) copied.

and on the second:

C:\>cp
Nothing to update - already has V2 script

Steve

Author

Commented:
Thank you very much for your hard work and time, the script worked a treat !!

Commented:
No problem, glad it helped.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial